Class WebApplicationContextUtils
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:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic WebApplicationContextFind a uniqueWebApplicationContextfor this web app: either the root web app context (preferred) or a uniqueWebApplicationContextamong the registeredServletContextattributes (typically coming from a singleDispatcherServletin the current web application).static WebApplicationContextFind the rootWebApplicationContextfor this web app, typically loaded viaContextLoaderListener.static WebApplicationContextFind the rootWebApplicationContextfor this web app, typically loaded viaContextLoaderListener.static WebApplicationContextgetWebApplicationContext(ServletContext sc, String attrName) Find a customWebApplicationContextfor this web app.static voidinitServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext) Convenient variant ofinitServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)that always providesnullfor theServletConfigparameter.static voidinitServletPropertySources(MutablePropertySources sources, ServletContext servletContext, ServletConfig servletConfig) ReplaceServlet-basedstub property sourceswith actual instances populated with the givenservletContextandservletConfigobjects.static voidRegister web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.static voidregisterEnvironmentBeans(ConfigurableListableBeanFactory bf, ServletContext servletContext, ServletConfig servletConfig) Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.static voidRegister web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebApplicationContext.static voidregisterWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, ServletContext sc) Register web-specific scopes ("request", "session", "globalSession", "application") with the given BeanFactory, as used by the WebApplicationContext.
- 
Constructor Details- 
WebApplicationContextUtilspublic WebApplicationContextUtils()
 
- 
- 
Method Details- 
getRequiredWebApplicationContextpublic static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException Find the rootWebApplicationContextfor this web app, typically loaded viaContextLoaderListener.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:
 
- 
getWebApplicationContextFind the rootWebApplicationContextfor this web app, typically loaded viaContextLoaderListener.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 nullif none
- See Also:
 
- 
getWebApplicationContext@Nullable public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) Find a customWebApplicationContextfor 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 nullif none
 
- 
findWebApplicationContextFind a uniqueWebApplicationContextfor this web app: either the root web app context (preferred) or a uniqueWebApplicationContextamong the registeredServletContextattributes (typically coming from a singleDispatcherServletin the current web application).Note that DispatcherServlet's exposure of its context can be controlled through itspublishContextproperty, which istrueby default but can be selectively switched to only publish a single context despite multipleDispatcherServletregistrations in the web app.- Parameters:
- sc- the ServletContext to find the web application context for
- Returns:
- the desired WebApplicationContext for this web app, or nullif none
- Since:
- 4.2
- See Also:
 
- 
registerWebApplicationScopesRegister web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebApplicationContext.- Parameters:
- beanFactory- the BeanFactory to configure
 
- 
registerWebApplicationScopespublic static void registerWebApplicationScopes(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
 
- 
registerEnvironmentBeanspublic static void registerEnvironmentBeans(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
 
- 
registerEnvironmentBeanspublic static void registerEnvironmentBeans(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
 
- 
initServletPropertySourcespublic static void initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext) Convenient variant ofinitServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)that always providesnullfor theServletConfigparameter.
- 
initServletPropertySourcespublic static void initServletPropertySources(MutablePropertySources sources, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) ReplaceServlet-basedstub property sourceswith actual instances populated with the givenservletContextandservletConfigobjects.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- MutablePropertySourcesto initialize (must not be- null)
- servletContext- the current- ServletContext(ignored if- nullor if the- servlet context property sourcehas already been initialized)
- servletConfig- the current- ServletConfig(ignored if- nullor if the- servlet config property sourcehas already been initialized)
- See Also:
 
 
-