Class GenericWebApplicationContext

All Implemented Interfaces:
Closeable, AutoCloseable, BeanFactory, HierarchicalBeanFactory, ListableBeanFactory, BeanDefinitionRegistry, ApplicationContext, ApplicationEventPublisher, ConfigurableApplicationContext, Lifecycle, MessageSource, AliasRegistry, EnvironmentCapable, ResourceLoader, ResourcePatternResolver, ThemeSource, ConfigurableWebApplicationContext, WebApplicationContext

public class GenericWebApplicationContext extends GenericApplicationContext implements ConfigurableWebApplicationContext, ThemeSource
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".

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.

Since:
1.2
Author:
Juergen Hoeller, Chris Beams, Sam Brannen