spring-framework / org.springframework.web.context.support / AnnotationConfigWebApplicationContext

AnnotationConfigWebApplicationContext

open class AnnotationConfigWebApplicationContext : AbstractRefreshableWebApplicationContext, AnnotationConfigRegistry

org.springframework.web.context.WebApplicationContext implementation which accepts annotated classes as input - in particular org.springframework.context.annotation.Configuration-annotated classes, but also plain org.springframework.stereotype.Component classes and JSR-330 compliant classes using javax.inject annotations. Allows for registering classes one by one (specifying class names as config location) as well as for classpath scanning (specifying base packages as config location).

This is essentially the equivalent of org.springframework.context.annotation.AnnotationConfigApplicationContext for a web environment.

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.

As of Spring 3.1, this class may also be directly instantiated and injected into Spring's DispatcherServlet or ContextLoaderListener when using the new org.springframework.web.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 org.springframework.context.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.

Author
Chris Beams

Author
Juergen Hoeller

Since
3.0

See Also
org.springframework.context.annotation.AnnotationConfigApplicationContext

Constructors

<init>

AnnotationConfigWebApplicationContext()

org.springframework.web.context.WebApplicationContext implementation which accepts annotated classes as input - in particular org.springframework.context.annotation.Configuration-annotated classes, but also plain org.springframework.stereotype.Component classes and JSR-330 compliant classes using javax.inject annotations. Allows for registering classes one by one (specifying class names as config location) as well as for classpath scanning (specifying base packages as config location).

This is essentially the equivalent of org.springframework.context.annotation.AnnotationConfigApplicationContext for a web environment.

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.

As of Spring 3.1, this class may also be directly instantiated and injected into Spring's DispatcherServlet or ContextLoaderListener when using the new org.springframework.web.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 org.springframework.context.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.

Functions

register

open fun register(vararg annotatedClasses: Class<*>): Unit

Register one or more annotated classes to be processed.

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

scan

open fun scan(vararg basePackages: String): Unit

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.

setBeanNameGenerator

open fun setBeanNameGenerator(beanNameGenerator: BeanNameGenerator): Unit

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

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

setScopeMetadataResolver

open fun setScopeMetadataResolver(scopeMetadataResolver: ScopeMetadataResolver): Unit

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

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