Top Description Inners Fields Constructors Methods
org.springframework.web.context.support

public abstract Class WebApplicationContextUtils

extends Object
Class Inheritance
Imports
java.io.Serializable, java.util.Collections, .Enumeration, .HashMap, .Map, jakarta.faces.context.ExternalContext, .FacesContext, jakarta.servlet.ServletConfig, .ServletContext, .ServletRequest, .ServletResponse, jakarta.servlet.http.HttpSession, org.springframework.beans.factory.ObjectFactory, org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.core.env.MutablePropertySources, .PropertySource.StubPropertySource, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, org.springframework.web.context.ConfigurableWebApplicationContext, .WebApplicationContext, org.springframework.web.context.request.RequestAttributes, .RequestContextHolder, .RequestScope, .ServletRequestAttributes, .ServletWebRequest, .SessionScope, .WebRequest

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.ContextLoader, org.springframework.web.servlet.FrameworkServlet, org.springframework.web.servlet.DispatcherServlet, org.springframework.web.jsf.FacesContextUtils, org.springframework.web.jsf.el.SpringBeanFacesELResolver

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
WebApplicationContextUtils.FacesDependencyRegistrar

Inner class to avoid hard-coded JSF dependency.

private static class
WebApplicationContextUtils.RequestObjectFactory

Factory that exposes the current request object on demand.

private static class
WebApplicationContextUtils.ResponseObjectFactory

Factory that exposes the current response object on demand.

private static class
WebApplicationContextUtils.SessionObjectFactory

Factory that exposes the current session object on demand.

private static class
WebApplicationContextUtils.WebRequestObjectFactory

Factory that exposes the current WebRequest object on demand.

Field Summary

Modifier and TypeField and Description
private static final boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static ServletRequestAttributes
currentRequestAttributes()

Return the current RequestAttributes instance as ServletRequestAttributes.

public static WebApplicationContext

Returns:

the desired WebApplicationContext for this web app, or null if none
findWebApplicationContext
(ServletContext
the ServletContext to find the web application context for
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).

public static WebApplicationContext

Returns:

the root WebApplicationContext for this web app
getRequiredWebApplicationContext
(ServletContext
the ServletContext to find the web application context for
sc
)

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

public static WebApplicationContext

Returns:

the root WebApplicationContext for this web app, or null if none
getWebApplicationContext
(ServletContext
the ServletContext to find the web application context for
sc
)

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

public static WebApplicationContext

Returns:

the desired WebApplicationContext for this web app, or null if none
getWebApplicationContext
(ServletContext
the ServletContext to find the web application context for
sc
,
String
the name of the ServletContext attribute to look for
attrName
)

Find a custom WebApplicationContext for this web app.

public static void
initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext)

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

public static void
initServletPropertySources(MutablePropertySources
the MutablePropertySources to initialize (must not be null)
sources
,
ServletContext
the current ServletContext (ignored if null or if the servlet context property source has already been initialized)
servletContext
,
ServletConfig
the current ServletConfig (ignored if null or if the servlet config property source has already been initialized)
servletConfig
)

Replace Servlet-based stub property sources with actual instances populated with the given servletContext and servletConfig objects.

public static void
registerEnvironmentBeans(ConfigurableListableBeanFactory
the BeanFactory to configure
bf
,
ServletContext
the ServletContext that we're running within
sc
)

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

public static void
registerEnvironmentBeans(ConfigurableListableBeanFactory
the BeanFactory to configure
bf
,
ServletContext
the ServletContext that we're running within
servletContext
,
ServletConfig
the ServletConfig
servletConfig
)

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

public static void
registerWebApplicationScopes(ConfigurableListableBeanFactory
the BeanFactory to configure
beanFactory
)

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

public static void
registerWebApplicationScopes(ConfigurableListableBeanFactory
the BeanFactory to configure
beanFactory
,
ServletContext
the ServletContext that we're running within
sc
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

jsfPresentback to summary
private static final boolean jsfPresent

Constructor Detail

WebApplicationContextUtilsback to summary
public WebApplicationContextUtils()

Method Detail

currentRequestAttributesback to summary
private static ServletRequestAttributes currentRequestAttributes()

Return the current RequestAttributes instance as ServletRequestAttributes.

See Also
RequestContextHolder#currentRequestAttributes()
findWebApplicationContextback to summary
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:ServletContext

the ServletContext to find the web application context for

Returns:WebApplicationContext

the desired WebApplicationContext for this web app, or null if none

Annotations
@Nullable
Since
4.2
See Also
getWebApplicationContext(ServletContext), ServletContext#getAttributeNames()
getRequiredWebApplicationContextback to summary
public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException

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.

Parameters
sc:ServletContext

the ServletContext to find the web application context for

Returns:WebApplicationContext

the root WebApplicationContext for this web app

Exceptions
IllegalStateException:
if the root WebApplicationContext could not be found
See Also
org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
getWebApplicationContextback to summary
public static WebApplicationContext getWebApplicationContext(ServletContext sc)

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.

Parameters
sc:ServletContext

the ServletContext to find the web application context for

Returns:WebApplicationContext

the root WebApplicationContext for this web app, or null if none

Annotations
@Nullable
See Also
org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
getWebApplicationContextback to summary
public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName)

Find a custom WebApplicationContext for this web app.

Parameters
sc:ServletContext

the ServletContext to find the web application context for

attrName:String

the name of the ServletContext attribute to look for

Returns:WebApplicationContext

the desired WebApplicationContext for this web app, or null if none

Annotations
@Nullable
initServletPropertySourcesback to summary
public static void initServletPropertySources(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)
initServletPropertySourcesback to summary
public static void initServletPropertySources(MutablePropertySources sources, ServletContext servletContext, 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:MutablePropertySources

the MutablePropertySources to initialize (must not be null)

servletContext:ServletContext

the current ServletContext (ignored if null or if the servlet context property source has already been initialized)

servletConfig:ServletConfig

the current ServletConfig (ignored if null or if the servlet config property source has already been initialized)

See Also
org.springframework.core.env.PropertySource.StubPropertySource, org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
registerEnvironmentBeansback to summary
public static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf, ServletContext sc)

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

Parameters
bf:ConfigurableListableBeanFactory

the BeanFactory to configure

sc:ServletContext

the ServletContext that we're running within

registerEnvironmentBeansback to summary
public static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf, ServletContext servletContext, ServletConfig servletConfig)

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

Parameters
bf:ConfigurableListableBeanFactory

the BeanFactory to configure

servletContext:ServletContext

the ServletContext that we're running within

servletConfig:ServletConfig

the ServletConfig

registerWebApplicationScopesback to summary
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory)

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

Parameters
beanFactory:ConfigurableListableBeanFactory

the BeanFactory to configure

registerWebApplicationScopesback to summary
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, ServletContext sc)

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

Parameters
beanFactory:ConfigurableListableBeanFactory

the BeanFactory to configure

sc:ServletContext

the ServletContext that we're running within

org.springframework.web.context.support back to summary

private Class WebApplicationContextUtils.FacesDependencyRegistrar

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.context.support.WebApplicationContextUtils.FacesDependencyRegistrar

Inner class to avoid hard-coded JSF dependency.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

FacesDependencyRegistrarback to summary
private FacesDependencyRegistrar()

Method Detail

registerFacesDependenciesback to summary
public static void registerFacesDependencies(ConfigurableListableBeanFactory beanFactory)
org.springframework.web.context.support back to summary

private Class WebApplicationContextUtils.RequestObjectFactory

extends Object
implements ObjectFactory<ServletRequest>, Serializable
Class Inheritance
  • java.lang.Object
  • org.springframework.web.context.support.WebApplicationContextUtils.RequestObjectFactory
All Implemented Interfaces
java.io.Serializable, org.springframework.beans.factory.ObjectFactory
Annotations
@SuppressWarnings:serial

Factory that exposes the current request object on demand.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public ServletRequest
getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

RequestObjectFactoryback to summary
private RequestObjectFactory()

Method Detail

getObjectback to summary
public ServletRequest getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Doc from org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

Returns:ServletRequest

the resulting instance

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.web.context.support back to summary

private Class WebApplicationContextUtils.ResponseObjectFactory

extends Object
implements ObjectFactory<ServletResponse>, Serializable
Class Inheritance
  • java.lang.Object
  • org.springframework.web.context.support.WebApplicationContextUtils.ResponseObjectFactory
All Implemented Interfaces
java.io.Serializable, org.springframework.beans.factory.ObjectFactory
Annotations
@SuppressWarnings:serial

Factory that exposes the current response object on demand.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public ServletResponse
getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

ResponseObjectFactoryback to summary
private ResponseObjectFactory()

Method Detail

getObjectback to summary
public ServletResponse getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Doc from org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

Returns:ServletResponse

the resulting instance

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.web.context.support back to summary

private Class WebApplicationContextUtils.SessionObjectFactory

extends Object
implements ObjectFactory<HttpSession>, Serializable
Class Inheritance
  • java.lang.Object
  • org.springframework.web.context.support.WebApplicationContextUtils.SessionObjectFactory
All Implemented Interfaces
java.io.Serializable, org.springframework.beans.factory.ObjectFactory
Annotations
@SuppressWarnings:serial

Factory that exposes the current session object on demand.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public HttpSession
getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

SessionObjectFactoryback to summary
private SessionObjectFactory()

Method Detail

getObjectback to summary
public HttpSession getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Doc from org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

Returns:HttpSession

the resulting instance

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.web.context.support back to summary

private Class WebApplicationContextUtils.WebRequestObjectFactory

extends Object
implements ObjectFactory<WebRequest>, Serializable
Class Inheritance
  • java.lang.Object
  • org.springframework.web.context.support.WebApplicationContextUtils.WebRequestObjectFactory
All Implemented Interfaces
java.io.Serializable, org.springframework.beans.factory.ObjectFactory
Annotations
@SuppressWarnings:serial

Factory that exposes the current WebRequest object on demand.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public WebRequest
getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

WebRequestObjectFactoryback to summary
private WebRequestObjectFactory()

Method Detail

getObjectback to summary
public WebRequest getObject()

Implements org.springframework.beans.factory.ObjectFactory.getObject.

Doc from org.springframework.beans.factory.ObjectFactory.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

Returns:WebRequest

the resulting instance

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override