类 WebApplicationContextUtils
- java.lang.Object
- org.springframework.web.context.support.WebApplicationContextUtils
public abstract class WebApplicationContextUtils extends Object
Convenience methods for retrieving the rootWebApplicationContext
for a givenServletContext
. 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.
- 作者:
- Juergen Hoeller
- 另请参阅:
ContextLoader
,FrameworkServlet
,DispatcherServlet
,FacesContextUtils
,SpringBeanFacesELResolver
构造器概要
构造器 构造器 说明 WebApplicationContextUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static WebApplicationContext
findWebApplicationContext(ServletContext sc)
Find a uniqueWebApplicationContext
for this web app: either the root web app context (preferred) or a uniqueWebApplicationContext
among the registeredServletContext
attributes (typically coming from a singleDispatcherServlet
in the current web application).static WebApplicationContext
getRequiredWebApplicationContext(ServletContext sc)
Find the rootWebApplicationContext
for this web app, typically loaded viaContextLoaderListener
.static WebApplicationContext
getWebApplicationContext(ServletContext sc)
Find the rootWebApplicationContext
for this web app, typically loaded viaContextLoaderListener
.static WebApplicationContext
getWebApplicationContext(ServletContext sc, String attrName)
Find a customWebApplicationContext
for this web app.static void
initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext)
Convenient variant ofinitServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)
that always providesnull
for theServletConfig
parameter.static void
initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig)
ReplaceServlet
-basedstub property sources
with actual instances populated with the givenservletContext
andservletConfig
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.
构造器详细资料
WebApplicationContextUtils
public WebApplicationContextUtils()
方法详细资料
getRequiredWebApplicationContext
public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException
Find the rootWebApplicationContext
for 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.
- 参数:
sc
- ServletContext to find the web application context for- 返回:
- the root WebApplicationContext for this web app
- 抛出:
IllegalStateException
- if the root WebApplicationContext could not be found- 另请参阅:
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
getWebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc)
Find the rootWebApplicationContext
for 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.
- 参数:
sc
- ServletContext to find the web application context for- 返回:
- the root WebApplicationContext for this web app, or
null
if none - 另请参阅:
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
getWebApplicationContext
public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName)
Find a customWebApplicationContext
for this web app.- 参数:
sc
- ServletContext to find the web application context forattrName
- the name of the ServletContext attribute to look for- 返回:
- the desired WebApplicationContext for this web app, or
null
if none
findWebApplicationContext
public static WebApplicationContext findWebApplicationContext(ServletContext sc)
Find a uniqueWebApplicationContext
for this web app: either the root web app context (preferred) or a uniqueWebApplicationContext
among the registeredServletContext
attributes (typically coming from a singleDispatcherServlet
in the current web application).Note that
DispatcherServlet
's exposure of its context can be controlled through itspublishContext
property, which istrue
by default but can be selectively switched to only publish a single context despite multipleDispatcherServlet
registrations in the web app.- 参数:
sc
- ServletContext to find the web application context for- 返回:
- the desired WebApplicationContext for this web app, or
null
if none - 从以下版本开始:
- 4.2
- 另请参阅:
getWebApplicationContext(ServletContext)
,ServletContext.getAttributeNames()
registerWebApplicationScopes
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory)
Register web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebApplicationContext.- 参数:
beanFactory
- the BeanFactory to configure
registerWebApplicationScopes
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.- 参数:
beanFactory
- the BeanFactory to configuresc
- the ServletContext that we're running within
registerEnvironmentBeans
public static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf, ServletContext sc)
Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebApplicationContext.- 参数:
bf
- the BeanFactory to configuresc
- the ServletContext that we're running within
registerEnvironmentBeans
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.- 参数:
bf
- the BeanFactory to configureservletContext
- the ServletContext that we're running withinservletConfig
- the ServletConfig of the containing Portlet
initServletPropertySources
public static void initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext)
Convenient variant ofinitServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)
that always providesnull
for theServletConfig
parameter.
initServletPropertySources
public static void initServletPropertySources(MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig)
ReplaceServlet
-basedstub property sources
with actual instances populated with the givenservletContext
andservletConfig
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
- theMutablePropertySources
to initialize (must not benull
)servletContext
- the currentServletContext
(ignored ifnull
or if theservlet context property source
has already been initialized)servletConfig
- the currentServletConfig
(ignored ifnull
or if theservlet config property source
has already been initialized)- 另请参阅:
PropertySource.StubPropertySource
,ConfigurableEnvironment.getPropertySources()