类 WebApplicationContextUtils
WebApplicationContext for
a given ServletContext. This is useful for programmatically accessing
a 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 Infra or available as an external library. This helper class is just the most generic way to access the root context.
- 从以下版本开始:
- 4.0 2022/2/20 17:12
- 作者:
- Juergen Hoeller, Harry Yang
-
嵌套类概要
嵌套类修饰符和类型类说明private static classFactory that exposes the current request object on demand.private static classFactory that exposes the current response object on demand.private static classFactory that exposes the current session object on demand. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static WebApplicationContextFind a uniqueWebServletApplicationContextfor this web app: either the root web app context (preferred) or a uniqueWebServletApplicationContextamong the registeredServletContextattributes (typically coming from a singleDispatcherServletin the current web application).static WebApplicationContextFind the rootWebServletApplicationContextfor this web app, typically loaded viaContextLoaderListener.static WebApplicationContextFind the rootWebServletApplicationContextfor this web app, typically loaded viaContextLoaderListener.static WebApplicationContextgetWebApplicationContext(ServletContext sc, String attrName) Find a customWebServletApplicationContextfor this web app.static voidinitServletPropertySources(cn.taketoday.core.env.PropertySources propertySources, ServletContext servletContext) Convenient variant ofinitServletPropertySources(PropertySources, ServletContext, ServletConfig)that always providesnullfor theServletConfigparameter.static voidinitServletPropertySources(cn.taketoday.core.env.PropertySources sources, ServletContext servletContext, ServletConfig servletConfig) ReplaceServlet-basedstub property sourceswith actual instances populated with the givenservletContextandservletConfigobjects.static voidregisterEnvironmentBeans(cn.taketoday.beans.factory.config.ConfigurableBeanFactory bf, ServletContext sc) Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebServletApplicationContext.static voidregisterEnvironmentBeans(cn.taketoday.beans.factory.config.ConfigurableBeanFactory bf, ServletContext servletContext, ServletConfig servletConfig) Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebServletApplicationContext.static voidregisterWebApplicationScopes(cn.taketoday.beans.factory.config.ConfigurableBeanFactory beanFactory) Register web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebServletApplicationContext.static voidregisterWebApplicationScopes(cn.taketoday.beans.factory.config.ConfigurableBeanFactory beanFactory, ServletContext sc) Register web-specific scopes ("request", "session") with the given BeanFactory, as used by the WebServletApplicationContext.
-
构造器详细资料
-
WebApplicationContextUtils
public WebApplicationContextUtils()
-
-
方法详细资料
-
getRequiredWebApplicationContext
public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException Find the rootWebServletApplicationContextfor 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- the ServletContext to find the web application context for- 返回:
- the root WebServletApplicationContext for this web app
- 抛出:
IllegalStateException- if the root WebServletApplicationContext could not be found- 另请参阅:
-
getWebApplicationContext
Find the rootWebServletApplicationContextfor 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- the ServletContext to find the web application context for- 返回:
- the root WebServletApplicationContext for this web app, or
nullif none - 另请参阅:
-
getWebApplicationContext
@Nullable public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) Find a customWebServletApplicationContextfor this web app.- 参数:
sc- the ServletContext to find the web application context forattrName- the name of the ServletContext attribute to look for- 返回:
- the desired WebServletApplicationContext for this web app, or
nullif none
-
findWebApplicationContext
Find a uniqueWebServletApplicationContextfor this web app: either the root web app context (preferred) or a uniqueWebServletApplicationContextamong the registeredServletContextattributes (typically coming from a singleDispatcherServletin the current web application).Note that
DispatcherServlet's exposure of its context can be controlled through itspublishContextproperty, which istrueby default but can be selectively switched to only publish a single context despite multipleDispatcherServletregistrations in the web app.- 参数:
sc- the ServletContext to find the web application context for- 返回:
- the desired WebServletApplicationContext for this web app, or
nullif none - 另请参阅:
-
registerWebApplicationScopes
public static void registerWebApplicationScopes(cn.taketoday.beans.factory.config.ConfigurableBeanFactory beanFactory) Register web-specific scopes ("request", "session", "globalSession") with the given BeanFactory, as used by the WebServletApplicationContext.- 参数:
beanFactory- the BeanFactory to configure
-
registerWebApplicationScopes
public static void registerWebApplicationScopes(cn.taketoday.beans.factory.config.ConfigurableBeanFactory beanFactory, @Nullable ServletContext sc) Register web-specific scopes ("request", "session") with the given BeanFactory, as used by the WebServletApplicationContext.- 参数:
beanFactory- the BeanFactory to configuresc- the ServletContext that we're running within
-
registerEnvironmentBeans
public static void registerEnvironmentBeans(cn.taketoday.beans.factory.config.ConfigurableBeanFactory bf, @Nullable ServletContext sc) Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebServletApplicationContext.- 参数:
bf- the BeanFactory to configuresc- the ServletContext that we're running within
-
registerEnvironmentBeans
public static void registerEnvironmentBeans(cn.taketoday.beans.factory.config.ConfigurableBeanFactory bf, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) Register web-specific environment beans ("contextParameters", "contextAttributes") with the given BeanFactory, as used by the WebServletApplicationContext.- 参数:
bf- the BeanFactory to configureservletContext- the ServletContext that we're running withinservletConfig- the ServletConfig
-
initServletPropertySources
public static void initServletPropertySources(cn.taketoday.core.env.PropertySources propertySources, ServletContext servletContext) Convenient variant ofinitServletPropertySources(PropertySources, ServletContext, ServletConfig)that always providesnullfor theServletConfigparameter. -
initServletPropertySources
public static void initServletPropertySources(cn.taketoday.core.env.PropertySources sources, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) ReplaceServlet-basedstub property sourceswith actual instances populated with the givenservletContextandservletConfigobjects.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.
- 参数:
sources- thePropertySourcesto initialize (must not benull)servletContext- the currentServletContext(ignored ifnullor if theservlet context property sourcehas already been initialized)servletConfig- the currentServletConfig(ignored ifnullor if theservlet config property sourcehas already been initialized)- 另请参阅:
-
PropertySource.StubPropertySourceConfigurableEnvironment.getPropertySources()
-