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

public Class AnnotationConfigWebApplicationContext

extends AbstractRefreshableWebApplicationContext
implements AnnotationConfigRegistry
Class Inheritance
All Implemented Interfaces
org.springframework.context.annotation.AnnotationConfigRegistry
Imports
java.util.Collections, .LinkedHashSet, .Set, org.springframework.beans.factory.support.BeanNameGenerator, .DefaultListableBeanFactory, org.springframework.context.annotation.AnnotatedBeanDefinitionReader, .AnnotationConfigRegistry, .AnnotationConfigUtils, .ClassPathBeanDefinitionScanner, .ScopeMetadataResolver, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .StringUtils, org.springframework.web.context.ContextLoader

References Deprecated

AbstractRefreshableWebApplicationContext is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

WebApplicationContext implementation which accepts component classes as input — in particular @Configuration classes, but also plain @Component classes as well as JSR-330 compliant classes using jakarta.inject annotations.

Allows for registering classes one by one (specifying class names as config locations) as well as via classpath scanning (specifying base packages as config locations).

This is essentially the equivalent of AnnotationConfigApplicationContext for a web environment. However, in contrast to AnnotationConfigApplicationContext, this class does not extend GenericApplicationContext and therefore does not provide some of the convenient registerBean(...) methods available in a GenericApplicationContext. If you wish to register annotated component classes with a GenericApplicationContext in a web environment, you may use a GenericWebApplicationContext with an AnnotatedBeanDefinitionReader. See the Javadoc for GenericWebApplicationContext for details and an example.

To make use of this application context, the "contextClass" context-param for ContextLoader and/or "contextClass" init-param for FrameworkServlet must be set to the fully-qualified name of this class.

This class may also be directly instantiated and injected into Spring's DispatcherServlet or ContextLoaderListener when using the WebApplicationInitializer code-based alternative to web.xml. See its Javadoc for details and usage examples.

Unlike XmlWebApplicationContext, no default configuration class locations are assumed. Rather, it is a requirement to set the "contextConfigLocation" context-param for ContextLoader and/or "contextConfigLocation" init-param for FrameworkServlet. The param-value may contain both fully-qualified class names and base packages to scan for components. See loadBeanDefinitions for exact details on how these locations are processed.

As an alternative to setting the "contextConfigLocation" parameter, users may implement an ApplicationContextInitializer and set the "contextInitializerClasses" context-param / init-param. In such cases, users should favor the refresh() and scan(String...) methods over the setConfigLocation(String) method, which is primarily for use by ContextLoader.

Note

In case of multiple @Configuration classes, later @Bean definitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra @Configuration class.

Authors
Chris Beams, Juergen Hoeller, Sam Brannen
Since
3.0
See Also
org.springframework.context.annotation.AnnotationConfigApplicationContext, org.springframework.web.context.support.GenericWebApplicationContext

Field Summary

Modifier and TypeField and Description
private final Set<String>
private BeanNameGenerator
private final Set<Class<?>>
private ScopeMetadataResolver

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected AnnotatedBeanDefinitionReader
getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory
the bean factory to load bean definitions into
beanFactory
)

Build an AnnotatedBeanDefinitionReader for the given bean factory.

protected BeanNameGenerator
protected ClassPathBeanDefinitionScanner
getClassPathBeanDefinitionScanner(DefaultListableBeanFactory
the bean factory to load bean definitions into
beanFactory
)

Build a ClassPathBeanDefinitionScanner for the given bean factory.

protected ScopeMetadataResolver
protected void
loadBeanDefinitions(DefaultListableBeanFactory
the bean factory to load bean definitions into
beanFactory
)

Implements abstract org.springframework.context.support.AbstractRefreshableApplicationContext.loadBeanDefinitions.

Register a org.springframework.beans.factory.config.BeanDefinition for any classes specified by register(Class...) and scan any packages specified by scan(String...).

public void
register(Class<?>...
one or more component classes, e.g. @Configuration classes
componentClasses
)

Implements org.springframework.context.annotation.AnnotationConfigRegistry.register.

Register one or more component classes to be processed.

public void
scan(String...
the packages to check for component classes
basePackages
)

Implements org.springframework.context.annotation.AnnotationConfigRegistry.scan.

Perform a scan within the specified base packages.

public void
public void
Inherited from org.springframework.web.context.support.AbstractRefreshableWebApplicationContext:
createEnvironmentgetApplicationNamegetConfigLocationsgetNamespacegetResourceByPathgetResourcePatternResolvergetServletConfiggetServletContextgetThemeinitPropertySourcesonRefreshpostProcessBeanFactorysetNamespacesetServletConfigsetServletContext

Field Detail

basePackagesback to summary
private final Set<String> basePackages
beanNameGeneratorback to summary
private BeanNameGenerator beanNameGenerator
Annotations
@Nullable
componentClassesback to summary
private final Set<Class<?>> componentClasses
scopeMetadataResolverback to summary
private ScopeMetadataResolver scopeMetadataResolver
Annotations
@Nullable

Constructor Detail

AnnotationConfigWebApplicationContextback to summary
public AnnotationConfigWebApplicationContext()

Method Detail

getAnnotatedBeanDefinitionReaderback to summary
protected AnnotatedBeanDefinitionReader getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory beanFactory)

Build an AnnotatedBeanDefinitionReader for the given bean factory.

This should be pre-configured with the Environment (if desired) but not with a BeanNameGenerator or ScopeMetadataResolver yet.

Parameters
beanFactory:DefaultListableBeanFactory

the bean factory to load bean definitions into

Since
4.1.9
See Also
getEnvironment(), getBeanNameGenerator(), getScopeMetadataResolver()
getBeanNameGeneratorback to summary
protected BeanNameGenerator getBeanNameGenerator()

Return the custom BeanNameGenerator for use with AnnotatedBeanDefinitionReader and/or ClassPathBeanDefinitionScanner, if any.

Annotations
@Nullable
getClassPathBeanDefinitionScannerback to summary
protected ClassPathBeanDefinitionScanner getClassPathBeanDefinitionScanner(DefaultListableBeanFactory beanFactory)

Build a ClassPathBeanDefinitionScanner for the given bean factory.

This should be pre-configured with the Environment (if desired) but not with a BeanNameGenerator or ScopeMetadataResolver yet.

Parameters
beanFactory:DefaultListableBeanFactory

the bean factory to load bean definitions into

Since
4.1.9
See Also
getEnvironment(), getBeanNameGenerator(), getScopeMetadataResolver()
getScopeMetadataResolverback to summary
protected ScopeMetadataResolver getScopeMetadataResolver()

Return the custom ScopeMetadataResolver for use with AnnotatedBeanDefinitionReader and/or ClassPathBeanDefinitionScanner, if any.

Annotations
@Nullable
loadBeanDefinitionsback to summary
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)

Implements abstract org.springframework.context.support.AbstractRefreshableApplicationContext.loadBeanDefinitions.

Register a org.springframework.beans.factory.config.BeanDefinition for any classes specified by register(Class...) and scan any packages specified by scan(String...).

For any values specified by setConfigLocation(String) or setConfigLocations(String[]), attempt first to load each location as a class, registering a BeanDefinition if class loading is successful, and if class loading fails (i.e. a ClassNotFoundException is raised), assume the value is a package and attempt to scan it for component classes.

Enables the default set of annotation configuration post processors, such that @Autowired and associated annotations can be used.

Configuration class bean definitions are registered with generated bean definition names unless the value attribute is provided to the stereotype annotation.

Parameters
beanFactory:DefaultListableBeanFactory

the bean factory to load bean definitions into

Annotations
@Override
See Also
register(Class...), scan(String...), setConfigLocation(String), setConfigLocations(String[]), AnnotatedBeanDefinitionReader, ClassPathBeanDefinitionScanner
registerback to summary
public void register(Class<?>... componentClasses)

Implements org.springframework.context.annotation.AnnotationConfigRegistry.register.

Register one or more component classes to be processed.

Note that refresh() must be called in order for the context to fully process the new classes.

Parameters
componentClasses:Class<?>[]

one or more component classes, e.g. @Configuration classes

Annotations
@Override
See Also
scan(String...), loadBeanDefinitions(DefaultListableBeanFactory), setConfigLocation(String), refresh()
scanback to summary
public void scan(String... basePackages)

Implements org.springframework.context.annotation.AnnotationConfigRegistry.scan.

Perform a scan within the specified base packages.

Note that refresh() must be called in order for the context to fully process the new classes.

Parameters
basePackages:String[]

the packages to check for component classes

Annotations
@Override
See Also
loadBeanDefinitions(DefaultListableBeanFactory), register(Class...), setConfigLocation(String), refresh()
setBeanNameGeneratorback to summary
public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)

Set a custom BeanNameGenerator for use with AnnotatedBeanDefinitionReader and/or ClassPathBeanDefinitionScanner.

Default is org.springframework.context.annotation.AnnotationBeanNameGenerator.

See Also
AnnotatedBeanDefinitionReader#setBeanNameGenerator, ClassPathBeanDefinitionScanner#setBeanNameGenerator
setScopeMetadataResolverback to summary
public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)

Set a custom ScopeMetadataResolver for use with AnnotatedBeanDefinitionReader and/or ClassPathBeanDefinitionScanner.

Default is an org.springframework.context.annotation.AnnotationScopeMetadataResolver.

See Also
AnnotatedBeanDefinitionReader#setScopeMetadataResolver, ClassPathBeanDefinitionScanner#setScopeMetadataResolver