Package org.springframework.web.context.support

# Class WebApplicationContextUtils

java.lang.Object
org.springframework.web.context.support.WebApplicationContextUtils

public abstract class WebApplicationContextUtils extends Object
Convenience methods for retrieving the root `WebApplicationContext` for a given `ServletContext`. This is useful for programmatically accessing a Spring application context from within custom web views or MVC actions.

Note that there are more convenient ways of accessing the root context for many web frameworks, either part of Spring or available as an external library. This helper class is just the most generic way to access the root context.

Author:
  Juergen Hoeller
See Also:
  - `ContextLoader`
  - `org.springframework.web.servlet.FrameworkServlet`
  - `org.springframework.web.servlet.DispatcherServlet`
  - `FacesContextUtils`
  - `SpringBeanFacesELResolver`

## Constructor Summary

Constructors
Constructor
Description
`WebApplicationContextUtils()`

## Method Summary

Modifier and Type
Method
Description
`static WebApplicationContext`
`findWebApplicationContext(ServletContext sc)`
Find a unique `WebApplicationContext` for this web app: either the root web app context (preferred) or a unique `WebApplicationContext` among the registered `ServletContext` attributes (typically coming from a single `DispatcherServlet` in the current web application).
`static WebApplicationContext`
`getRequiredWebApplicationContext(ServletContext sc)`
Find the root `WebApplicationContext` for this web app, typically loaded via `ContextLoaderListener`.
`static WebApplicationContext`
`getWebApplicationContext(ServletContext sc)`
Find the root `WebApplicationContext` for this web app, typically loaded via `ContextLoaderListener`.
`static WebApplicationContext`
`getWebApplicationContext(ServletContext sc, String attrName)`
Find a custom `WebApplicationContext` for this web app.
`static void`
`initServletPropertySources(org.springframework.core.env.MutablePropertySources propertySources, ServletContext servletContext)`
Convenient variant of `initServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)` that always provides `null` for the `ServletConfig` parameter.
`static void`
`initServletPropertySources(org.springframework.core.env.MutablePropertySources sources, ServletContext servletContext, ServletConfig servletConfig)`
Replace `Servlet`-based `stub property sources` with actual instances populated with the given `servletContext` and `servletConfig` objects.
`static void`
`registerEnvironmentBeans(org.springframework.beans.factory.config.ConfigurableListableBeanFactory bf, ServletContext sc)`
Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.
`static void`
`registerEnvironmentBeans(org.springframework.beans.factory.config.ConfigurableListableBeanFactory bf, ServletContext servletContext, ServletConfig servletConfig)`
Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.
`static void`
`registerWebApplicationScopes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)`
Register web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebApplicationContext.
`static void`
`registerWebApplicationScopes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, ServletContext sc)`
Register web-specific scopes ("request", "session", "globalSession", "application") with the given BeanFactory, as used by the WebApplicationContext.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### WebApplicationContextUtils

public WebApplicationContextUtils()

## Method Details

### getRequiredWebApplicationContext

public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException
Find the root `WebApplicationContext` for this web app, typically loaded via `ContextLoaderListener`.

Will rethrow an exception that happened on root context startup, to differentiate between a failed context startup and no context at all.

Parameters:
  `sc` - the ServletContext to find the web application context for
Returns:
  the root WebApplicationContext for this web app
Throws:
  `IllegalStateException` - if the root WebApplicationContext could not be found
See Also:
  - `WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE`

### getWebApplicationContext

@Nullable public static WebApplicationContext getWebApplicationContext(ServletContext sc)
Find the root `WebApplicationContext` for this web app, typically loaded via `ContextLoaderListener`.

Will rethrow an exception that happened on root context startup, to differentiate between a failed context startup and no context at all.

Parameters:
  `sc` - the ServletContext to find the web application context for
Returns:
  the root WebApplicationContext for this web app, or `null` if none
See Also:
  - `WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE`

### getWebApplicationContext

@Nullable public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName)
Find a custom `WebApplicationContext` for this web app.

Parameters:
  `sc` - the ServletContext to find the web application context for
  `attrName` - the name of the ServletContext attribute to look for
Returns:
  the desired WebApplicationContext for this web app, or `null` if none

### findWebApplicationContext

@Nullable public static WebApplicationContext findWebApplicationContext(ServletContext sc)
Find a unique `WebApplicationContext` for this web app: either the root web app context (preferred) or a unique `WebApplicationContext` among the registered `ServletContext` attributes (typically coming from a single `DispatcherServlet` in the current web application).

Note that `DispatcherServlet`'s exposure of its context can be controlled through its `publishContext` property, which is `true` by default but can be selectively switched to only publish a single context despite multiple `DispatcherServlet` registrations in the web app.

Parameters:
  `sc` - the ServletContext to find the web application context for
Returns:
  the desired WebApplicationContext for this web app, or `null` if none
Since:
  4.2
See Also:
  - `getWebApplicationContext(ServletContext)`
  - `ServletContext.getAttributeNames()`

### registerWebApplicationScopes

public static void registerWebApplicationScopes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Register web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebApplicationContext.

Parameters:
  `beanFactory` - the BeanFactory to configure

### registerWebApplicationScopes

public static void registerWebApplicationScopes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, @Nullable ServletContext sc)
Register web-specific scopes ("request", "session", "globalSession", "application") with the given BeanFactory, as used by the WebApplicationContext.

Parameters:
  `beanFactory` - the BeanFactory to configure
  `sc` - the ServletContext that we're running within

### registerEnvironmentBeans

public static void registerEnvironmentBeans(org.springframework.beans.factory.config.ConfigurableListableBeanFactory bf, @Nullable ServletContext sc)
Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.

Parameters:
  `bf` - the BeanFactory to configure
  `sc` - the ServletContext that we're running within

### registerEnvironmentBeans

public static void registerEnvironmentBeans(org.springframework.beans.factory.config.ConfigurableListableBeanFactory bf, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig)
Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.

Parameters:
  `bf` - the BeanFactory to configure
  `servletContext` - the ServletContext that we're running within
  `servletConfig` - the ServletConfig

### initServletPropertySources

public static void initServletPropertySources(org.springframework.core.env.MutablePropertySources propertySources, ServletContext servletContext)
Convenient variant of `initServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)` that always provides `null` for the `ServletConfig` parameter.

See Also:
  - `initServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)`

### initServletPropertySources

public static void initServletPropertySources(org.springframework.core.env.MutablePropertySources sources, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig)
Replace `Servlet`-based `stub property sources` with actual instances populated with the given `servletContext` and `servletConfig` objects.

This method is idempotent with respect to the fact it may be called any number of times but will perform replacement of stub property sources with their corresponding actual property sources once and only once.

Parameters:
  `sources` - the `MutablePropertySources` to initialize (must not be `null`)
  `servletContext` - the current `ServletContext` (ignored if `null` or if the `servlet context property source` has already been initialized)
  `servletConfig` - the current `ServletConfig` (ignored if `null` or if the `servlet config property source` has already been initialized)
See Also:
  - `PropertySource.StubPropertySource`
  - `ConfigurableEnvironment.getPropertySources()`