public abstract class WebApplicationContextUtils extends Object
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.
ContextLoader
,
FrameworkServlet
,
DispatcherServlet
,
FacesContextUtils
,
SpringBeanFacesELResolver
Constructor and Description |
---|
WebApplicationContextUtils() |
Modifier and Type | Method and 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(MutablePropertySources propertySources,
ServletContext servletContext)
Convenient variant of
initServletPropertySources(MutablePropertySources,
ServletContext, ServletConfig) that always provides null for the
ServletConfig parameter. |
static void |
initServletPropertySources(MutablePropertySources propertySources,
ServletContext servletContext,
ServletConfig servletConfig)
Replace
Servlet -based stub property sources with
actual instances populated with the given servletContext and
servletConfig objects. |
static void |
registerEnvironmentBeans(ConfigurableListableBeanFactory bf,
ServletContext sc)
Register web-specific environment beans ("contextParameters", "contextAttributes")
with the given BeanFactory, as used by the WebApplicationContext.
|
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.
|
static void |
registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory)
Register web-specific scopes ("request", "session", "globalSession")
with the given BeanFactory, as used by the WebApplicationContext.
|
static void |
registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
ServletContext sc)
Register web-specific scopes ("request", "session", "globalSession", "application")
with the given BeanFactory, as used by the WebApplicationContext.
|
public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException
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.
sc
- ServletContext to find the web application context forIllegalStateException
- if the root WebApplicationContext could not be foundWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
public static WebApplicationContext getWebApplicationContext(ServletContext sc)
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.
sc
- ServletContext to find the web application context fornull
if noneWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName)
WebApplicationContext
for this web app.sc
- ServletContext to find the web application context forattrName
- the name of the ServletContext attribute to look fornull
if nonepublic static WebApplicationContext findWebApplicationContext(ServletContext sc)
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.
sc
- ServletContext to find the web application context fornull
if nonegetWebApplicationContext(ServletContext)
,
ServletContext.getAttributeNames()
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory)
beanFactory
- the BeanFactory to configurepublic static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, ServletContext sc)
beanFactory
- the BeanFactory to configuresc
- the ServletContext that we're running withinpublic static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf, ServletContext sc)
bf
- the BeanFactory to configuresc
- the ServletContext that we're running withinpublic static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf, ServletContext servletContext, ServletConfig servletConfig)
bf
- the BeanFactory to configureservletContext
- the ServletContext that we're running withinservletConfig
- the ServletConfig of the containing Portletpublic static void initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext)
initServletPropertySources(MutablePropertySources,
ServletContext, ServletConfig)
that always provides null
for the
ServletConfig
parameter.public static void initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig)
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.
propertySources
- 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)PropertySource.StubPropertySource
,
ConfigurableEnvironment.getPropertySources()