spring-framework / org.springframework.web.context.support / WebApplicationContextUtils

WebApplicationContextUtils

abstract class WebApplicationContextUtils

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
org.springframework.web.context.ContextLoaderorg.springframework.web.servlet.FrameworkServletorg.springframework.web.servlet.DispatcherServletorg.springframework.web.jsf.FacesContextUtilsorg.springframework.web.jsf.el.SpringBeanFacesELResolver

Constructors

<init>

WebApplicationContextUtils()

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.

Functions

findWebApplicationContext

open static fun findWebApplicationContext(sc: ServletContext): WebApplicationContext

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.

getRequiredWebApplicationContext

open static fun getRequiredWebApplicationContext(sc: ServletContext): WebApplicationContext

Find the root WebApplicationContext for this web app, typically loaded via org.springframework.web.context.ContextLoaderListener.

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

getWebApplicationContext

open static fun getWebApplicationContext(sc: ServletContext): WebApplicationContext

Find the root WebApplicationContext for this web app, typically loaded via org.springframework.web.context.ContextLoaderListener.

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

open static fun getWebApplicationContext(sc: ServletContext, attrName: String): WebApplicationContext

Find a custom WebApplicationContext for this web app.

initServletPropertySources

open static fun initServletPropertySources(propertySources: MutablePropertySources, servletContext: ServletContext): Unit

Convenient variant of #initServletPropertySources(MutablePropertySources, * ServletContext, ServletConfig) that always provides null for the ServletConfig parameter.

open static fun initServletPropertySources(sources: MutablePropertySources, servletContext: ServletContext, servletConfig: ServletConfig): Unit

Replace Servlet-based StubPropertySource 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.

registerEnvironmentBeans

open static fun registerEnvironmentBeans(bf: ConfigurableListableBeanFactory, sc: ServletContext): Unit
open static fun registerEnvironmentBeans(bf: ConfigurableListableBeanFactory, servletContext: ServletContext, servletConfig: ServletConfig): Unit

Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.

registerWebApplicationScopes

open static fun registerWebApplicationScopes(beanFactory: ConfigurableListableBeanFactory): Unit

Register web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebApplicationContext.

open static fun registerWebApplicationScopes(beanFactory: ConfigurableListableBeanFactory, sc: ServletContext): Unit

Register web-specific scopes ("request", "session", "globalSession", "application") with the given BeanFactory, as used by the WebApplicationContext.