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

public Class GenericWebApplicationContext

extends GenericApplicationContext
implements ConfigurableWebApplicationContext, ThemeSource
Class Inheritance
All Implemented Interfaces
org.springframework.ui.context.ThemeSource, org.springframework.web.context.ConfigurableWebApplicationContext, org.springframework.context.ConfigurableApplicationContext, java.io.Closeable, java.lang.AutoCloseable, org.springframework.context.Lifecycle, org.springframework.context.ApplicationContext, org.springframework.core.io.support.ResourcePatternResolver, org.springframework.core.io.ResourceLoader, org.springframework.context.ApplicationEventPublisher, org.springframework.context.MessageSource, org.springframework.beans.factory.HierarchicalBeanFactory, org.springframework.beans.factory.BeanFactory, org.springframework.beans.factory.ListableBeanFactory, org.springframework.core.env.EnvironmentCapable, org.springframework.web.context.WebApplicationContext
Annotations
@SuppressWarnings:deprecation
Imports
jakarta.servlet.ServletConfig, .ServletContext, org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.beans.factory.support.DefaultListableBeanFactory, org.springframework.context.support.GenericApplicationContext, org.springframework.core.env.ConfigurableEnvironment, org.springframework.core.io.Resource, org.springframework.core.io.support.ResourcePatternResolver, org.springframework.lang.Nullable, org.springframework.ui.context.Theme, .ThemeSource, org.springframework.ui.context.support.UiApplicationContextUtils, org.springframework.util.Assert, .ObjectUtils, .StringUtils, org.springframework.web.context.ConfigurableWebApplicationContext, .ConfigurableWebEnvironment, .ServletContextAware

Deprecated

as a consequence of ThemeSource being deprecated. See corresponding docs for further information.

Subclass of GenericApplicationContext, suitable for web environments.

Implements ConfigurableWebApplicationContext, but is not intended for declarative setup in web.xml. Instead, it is designed for programmatic setup, for example for building nested contexts or for use within WebApplicationInitializers.

Interprets resource paths as servlet context resources, i.e. as paths beneath the web application root. Absolute paths — for example, for files outside the web app root — can be accessed via file: URLs, as implemented by AbstractApplicationContext.

In addition to the special beans detected by AbstractApplicationContext, this class detects a ThemeSource bean in the context, with the name "themeSource". Theme support is deprecated as of 6.0 with no direct replacement.

If you wish to register annotated component classes with a GenericWebApplicationContext, you can use an AnnotatedBeanDefinitionReader, as demonstrated in the following example. Component classes include in particular @Configuration classes but also plain @Component classes as well as JSR-330 compliant classes using jakarta.inject annotations.

GenericWebApplicationContext context = new GenericWebApplicationContext();
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
reader.register(AppConfig.class, UserController.class, UserRepository.class);

If you intend to implement a WebApplicationContext that reads bean definitions from configuration files, consider deriving from AbstractRefreshableWebApplicationContext, reading the bean definitions in an implementation of the loadBeanDefinitions method.

Authors
Juergen Hoeller, Chris Beams, Sam Brannen
Since
1.2

Field Summary

Modifier and TypeField and Description
private ServletContext
private ThemeSource
themeSource

Deprecated as a consequence of ThemeSource being deprecated.

Constructor Summary

AccessConstructor and Description
public
GenericWebApplicationContext()

Create a new GenericWebApplicationContext.

public
GenericWebApplicationContext(ServletContext
the ServletContext to run in
servletContext
)

Create a new GenericWebApplicationContext for the given ServletContext.

public
GenericWebApplicationContext(DefaultListableBeanFactory
the DefaultListableBeanFactory instance to use for this context
beanFactory
)

Create a new GenericWebApplicationContext with the given DefaultListableBeanFactory.

public
GenericWebApplicationContext(DefaultListableBeanFactory
the DefaultListableBeanFactory instance to use for this context
beanFactory
,
ServletContext
the ServletContext to run in
servletContext
)

Create a new GenericWebApplicationContext with the given DefaultListableBeanFactory and ServletContext.

Method Summary

Modifier and TypeMethod and Description
protected ConfigurableEnvironment
public String
public String[]
getConfigLocations()

Implements org.springframework.web.context.ConfigurableWebApplicationContext.getConfigLocations.

Return the config locations for this web application context, or null if none specified.
public String
getNamespace()

Implements org.springframework.web.context.ConfigurableWebApplicationContext.getNamespace.

Return the namespace for this web application context, if any.
protected Resource
getResourceByPath(String
the path to the resource
path
)

Overrides org.springframework.core.io.DefaultResourceLoader.getResourceByPath.

This implementation supports file paths beneath the root of the ServletContext.
protected ResourcePatternResolver
getResourcePatternResolver()

Overrides org.springframework.context.support.AbstractApplicationContext.getResourcePatternResolver.

This implementation supports pattern matching in unexpanded WARs too.
public ServletConfig
getServletConfig()

Implements org.springframework.web.context.ConfigurableWebApplicationContext.getServletConfig.

Return the ServletConfig for this web application context, if any.
public ServletContext
getServletContext()

Implements org.springframework.web.context.WebApplicationContext.getServletContext.

Return the standard Servlet API ServletContext for this application.
public Theme
getTheme(String
the name of the theme
themeName
)

Implements org.springframework.ui.context.ThemeSource.getTheme.

Deprecated
Return the Theme instance for the given theme name.
protected void
initPropertySources()

Overrides org.springframework.context.support.AbstractApplicationContext.initPropertySources.

Replace any stub property sources with actual instances.

protected void
protected void
postProcessBeanFactory(ConfigurableListableBeanFactory
the bean factory used by the application context
beanFactory
)

Overrides org.springframework.context.support.AbstractApplicationContext.postProcessBeanFactory.

Register request/session scopes, environment beans, a ServletContextAwareProcessor, etc.
public void
setConfigLocation(String configLocation)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setConfigLocation.

Set the config locations for this web application context in init-param style, i.e. with distinct locations separated by commas, semicolons or whitespace.
public void
setConfigLocations(String... configLocations)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setConfigLocations.

Set the config locations for this web application context.
public void
setNamespace(String namespace)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setNamespace.

Set the namespace for this web application context, to be used for building a default context config location.
public void
setServletConfig(ServletConfig servletConfig)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setServletConfig.

Set the ServletConfig for this web application context.
public void
Inherited from org.springframework.context.support.GenericApplicationContext:
cancelRefreshcloseBeanFactorygetAutowireCapableBeanFactorygetBeanDefinitiongetBeanFactorygetClassLoadergetDefaultListableBeanFactorygetResourcegetResourcesisAliasisBeanDefinitionOverridableisBeanNameInUserefreshBeanFactoryrefreshForAotProcessingregisterAliasregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanDefinitionremoveAliasremoveBeanDefinitionsetAllowBeanDefinitionOverridingsetAllowCircularReferencessetApplicationStartupsetClassLoadersetParentsetResourceLoader

Field Detail

servletContextback to summary
private ServletContext servletContext
Annotations
@Nullable
themeSourceback to summary
private ThemeSource themeSource

Deprecated

as a consequence of ThemeSource being deprecated. See corresponding docs for further information.

Annotations
@Nullable

Constructor Detail

GenericWebApplicationContextback to summary
public GenericWebApplicationContext()

Create a new GenericWebApplicationContext.

See Also
setServletContext, registerBeanDefinition, refresh
GenericWebApplicationContextback to summary
public GenericWebApplicationContext(ServletContext servletContext)

Create a new GenericWebApplicationContext for the given ServletContext.

Parameters
servletContext:ServletContext

the ServletContext to run in

See Also
registerBeanDefinition, refresh
GenericWebApplicationContextback to summary
public GenericWebApplicationContext(DefaultListableBeanFactory beanFactory)

Create a new GenericWebApplicationContext with the given DefaultListableBeanFactory.

Parameters
beanFactory:DefaultListableBeanFactory

the DefaultListableBeanFactory instance to use for this context

See Also
setServletContext, registerBeanDefinition, refresh
GenericWebApplicationContextback to summary
public GenericWebApplicationContext(DefaultListableBeanFactory beanFactory, ServletContext servletContext)

Create a new GenericWebApplicationContext with the given DefaultListableBeanFactory and ServletContext.

Parameters
beanFactory:DefaultListableBeanFactory

the DefaultListableBeanFactory instance to use for this context

servletContext:ServletContext

the ServletContext to run in

See Also
registerBeanDefinition, refresh

Method Detail

createEnvironmentback to summary
protected ConfigurableEnvironment createEnvironment()

Overrides org.springframework.context.support.AbstractApplicationContext.createEnvironment.

Create and return a new StandardServletEnvironment.

Annotations
@Override
getApplicationNameback to summary
public String getApplicationName()

Overrides org.springframework.context.support.AbstractApplicationContext.getApplicationName.

Implements org.springframework.context.ApplicationContext.getApplicationName.

Doc from org.springframework.context.ApplicationContext.getApplicationName.

Return a name for the deployed application that this context belongs to.

Returns:String

a name for the deployed application, or the empty String by default

Annotations
@Override
getConfigLocationsback to summary
public String[] getConfigLocations()

Implements org.springframework.web.context.ConfigurableWebApplicationContext.getConfigLocations.

Doc from org.springframework.web.context.ConfigurableWebApplicationContext.getConfigLocations.

Return the config locations for this web application context, or null if none specified.

Annotations
@Override
getNamespaceback to summary
public String getNamespace()

Implements org.springframework.web.context.ConfigurableWebApplicationContext.getNamespace.

Doc from org.springframework.web.context.ConfigurableWebApplicationContext.getNamespace.

Return the namespace for this web application context, if any.

Annotations
@Override
@Nullable
getResourceByPathback to summary
protected Resource getResourceByPath(String path)

Overrides org.springframework.core.io.DefaultResourceLoader.getResourceByPath.

This implementation supports file paths beneath the root of the ServletContext.

Parameters
path:String

Doc from org.springframework.core.io.DefaultResourceLoader.getResourceByPath.

the path to the resource

Returns:Resource

Doc from org.springframework.core.io.DefaultResourceLoader.getResourceByPath.

the corresponding Resource handle

Annotations
@Override
See Also
ServletContextResource
getResourcePatternResolverback to summary
protected ResourcePatternResolver getResourcePatternResolver()

Overrides org.springframework.context.support.AbstractApplicationContext.getResourcePatternResolver.

This implementation supports pattern matching in unexpanded WARs too.

Returns:ResourcePatternResolver

Doc from org.springframework.context.support.AbstractApplicationContext.getResourcePatternResolver.

the ResourcePatternResolver for this context

Annotations
@Override
See Also
ServletContextResourcePatternResolver
getServletConfigback to summary
public ServletConfig getServletConfig()

Implements org.springframework.web.context.ConfigurableWebApplicationContext.getServletConfig.

Doc from org.springframework.web.context.ConfigurableWebApplicationContext.getServletConfig.

Return the ServletConfig for this web application context, if any.

Annotations
@Override
@Nullable
getServletContextback to summary
public ServletContext getServletContext()

Implements org.springframework.web.context.WebApplicationContext.getServletContext.

Doc from org.springframework.web.context.WebApplicationContext.getServletContext.

Return the standard Servlet API ServletContext for this application.

Annotations
@Override
@Nullable
getThemeback to summary
public Theme getTheme(String themeName)

Implements org.springframework.ui.context.ThemeSource.getTheme.

Doc from org.springframework.ui.context.ThemeSource.getTheme.

Deprecated

Return the Theme instance for the given theme name.

The returned Theme will resolve theme-specific messages, codes, file paths, etc (e.g. CSS and image files in a web environment).

Parameters
themeName:String

the name of the theme

Returns:Theme

the corresponding Theme, or null if none defined. Note that, by convention, a ThemeSource should at least be able to return a default Theme for the default theme name "theme" but may also return default Themes for other theme names.

Annotations
@Override
@Nullable
@Deprecated
initPropertySourcesback to summary
protected void initPropertySources()

Overrides org.springframework.context.support.AbstractApplicationContext.initPropertySources.

Doc from org.springframework.context.support.AbstractApplicationContext.initPropertySources.

Replace any stub property sources with actual instances.

Replace Servlet-related property sources.

Annotations
@Override
onRefreshback to summary
protected void onRefresh()

Overrides org.springframework.context.support.AbstractApplicationContext.onRefresh.

Initialize the theme capability.

Annotations
@Override
postProcessBeanFactoryback to summary
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

Overrides org.springframework.context.support.AbstractApplicationContext.postProcessBeanFactory.

Register request/session scopes, environment beans, a ServletContextAwareProcessor, etc.

Parameters
beanFactory:ConfigurableListableBeanFactory

Doc from org.springframework.context.support.AbstractApplicationContext.postProcessBeanFactory.

the bean factory used by the application context

Annotations
@Override
setConfigLocationback to summary
public void setConfigLocation(String configLocation)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setConfigLocation.

Doc from org.springframework.web.context.ConfigurableWebApplicationContext.setConfigLocation.

Set the config locations for this web application context in init-param style, i.e. with distinct locations separated by commas, semicolons or whitespace.

If not set, the implementation is supposed to use a default for the given namespace or the root web application context, as appropriate.

Annotations
@Override
setConfigLocationsback to summary
public void setConfigLocations(String... configLocations)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setConfigLocations.

Doc from org.springframework.web.context.ConfigurableWebApplicationContext.setConfigLocations.

Set the config locations for this web application context.

If not set, the implementation is supposed to use a default for the given namespace or the root web application context, as appropriate.

Annotations
@Override
setNamespaceback to summary
public void setNamespace(String namespace)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setNamespace.

Doc from org.springframework.web.context.ConfigurableWebApplicationContext.setNamespace.

Set the namespace for this web application context, to be used for building a default context config location. The root web application context does not have a namespace.

Annotations
@Override
setServletConfigback to summary
public void setServletConfig(ServletConfig servletConfig)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setServletConfig.

Doc from org.springframework.web.context.ConfigurableWebApplicationContext.setServletConfig.

Set the ServletConfig for this web application context. Only called for a WebApplicationContext that belongs to a specific Servlet.

Annotations
@Override
setServletContextback to summary
public void setServletContext(ServletContext servletContext)

Implements org.springframework.web.context.ConfigurableWebApplicationContext.setServletContext.

Set the ServletContext that this WebApplicationContext runs in.

Annotations
@Override