public abstract class WebApplicationContextUtils extends Object
WebApplicationContext
for a given
ServletContext
. This is e.g. useful for accessing a Spring
context from within custom web views or Struts actions.
Note that there are more convenient ways of accessing the root context for many web frameworks, either part of Spring or available as external library. This helper class is just the most generic way to access the root context.
ContextLoader
,
FrameworkServlet
,
DispatcherServlet
,
FacesContextUtils
,
SpringBeanVariableResolver
,
SpringBeanFacesELResolver
Constructor and Description |
---|
WebApplicationContextUtils() |
Modifier and Type | Method and Description |
---|---|
static WebApplicationContext |
getRequiredWebApplicationContext(javax.servlet.ServletContext sc)
Find the root WebApplicationContext for this web application, which is
typically loaded via
ContextLoaderListener . |
static WebApplicationContext |
getWebApplicationContext(javax.servlet.ServletContext sc)
Find the root WebApplicationContext for this web application, which is
typically loaded via
ContextLoaderListener . |
static WebApplicationContext |
getWebApplicationContext(javax.servlet.ServletContext sc,
String attrName)
Find a custom WebApplicationContext for this web application.
|
static void |
initServletPropertySources(MutablePropertySources propertySources,
javax.servlet.ServletContext servletContext)
Replace
Servlet -based stub property sources with actual instances
populated with the given context object. |
static void |
initServletPropertySources(MutablePropertySources propertySources,
javax.servlet.ServletContext servletContext,
javax.servlet.ServletConfig servletConfig)
Replace
Servlet -based stub property sources with actual instances
populated with the given context and config objects. |
static void |
registerEnvironmentBeans(ConfigurableListableBeanFactory bf,
javax.servlet.ServletContext sc)
Register web-specific environment beans ("contextParameters", "contextAttributes")
with the given BeanFactory, as used by the WebApplicationContext.
|
static void |
registerEnvironmentBeans(ConfigurableListableBeanFactory bf,
javax.servlet.ServletContext sc,
javax.servlet.ServletConfig config)
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,
javax.servlet.ServletContext sc)
Register web-specific scopes ("request", "session", "globalSession", "application")
with the given BeanFactory, as used by the WebApplicationContext.
|
public static WebApplicationContext getRequiredWebApplicationContext(javax.servlet.ServletContext sc) throws IllegalStateException
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(javax.servlet.ServletContext sc)
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(javax.servlet.ServletContext sc, String attrName)
sc
- ServletContext to find the web application context forattrName
- the name of the ServletContext attribute to look fornull
if nonepublic static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory)
beanFactory
- the BeanFactory to configurepublic static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, javax.servlet.ServletContext sc)
beanFactory
- the BeanFactory to configuresc
- the ServletContext that we're running withinpublic static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf, javax.servlet.ServletContext sc)
bf
- the BeanFactory to configuresc
- the ServletContext that we're running withinpublic static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf, javax.servlet.ServletContext sc, javax.servlet.ServletConfig config)
bf
- the BeanFactory to configuresc
- the ServletContext that we're running withinconfig
- the ServletConfig of the containing Portletpublic static void initServletPropertySources(MutablePropertySources propertySources, javax.servlet.ServletContext servletContext)
Servlet
-based stub property sources with actual instances
populated with the given context object.public static void initServletPropertySources(MutablePropertySources propertySources, javax.servlet.ServletContext servletContext, javax.servlet.ServletConfig servletConfig)
Servlet
-based stub property sources with actual instances
populated with the given context and config objects.Copyright © 2014. All Rights Reserved.