public class

AnnotationConfigApplicationContext

extends GenericApplicationContext
java.lang.Object
   ↳ org.springframework.core.io.DefaultResourceLoader
     ↳ org.springframework.context.support.AbstractApplicationContext
       ↳ org.springframework.context.support.GenericApplicationContext
         ↳ org.springframework.context.annotation.AnnotationConfigApplicationContext

Class Overview

Standalone application context, accepting annotated classes as input - in particular @Configuration-annotated classes, but also plain @Components and JSR-330 compliant classes using javax.inject annotations. Allows for registering classes one by one (register(Class...)) as well as for classpath scanning (scan(String...)).

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

Summary

[Expand]
Inherited Constants
From class org.springframework.context.support.AbstractApplicationContext
From interface org.springframework.beans.factory.BeanFactory
From interface org.springframework.context.ConfigurableApplicationContext
From interface org.springframework.core.io.ResourceLoader
From interface org.springframework.core.io.support.ResourcePatternResolver
[Expand]
Inherited Fields
From class org.springframework.context.support.AbstractApplicationContext
Public Constructors
AnnotationConfigApplicationContext()
Create a new AnnotationConfigApplicationContext that needs to be populated through register(Class...) calls and then manually refreshed.
AnnotationConfigApplicationContext(Class...<?> annotatedClasses)
Create a new AnnotationConfigApplicationContext, deriving bean definitions from the given annotated classes and automatically refreshing the context.
AnnotationConfigApplicationContext(String... basePackages)
Create a new AnnotationConfigApplicationContext, scanning for bean definitions in the given packages and automatically refreshing the context.
Public Methods
void register(Class...<?> annotatedClasses)
Register an annotated class to be processed.
void scan(String... basePackages)
Perform a scan within the specified base packages.
void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Set the BeanNameGenerator to use for detected bean classes.
void setEnvironment(ConfigurableEnvironment environment)
Set the Environment for this application context.

Default value is determined by createEnvironment().

Delegates given environment to underlying AnnotatedBeanDefinitionReader and ClassPathBeanDefinitionScanner members.

void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
Set the ScopeMetadataResolver to use for detected bean classes.
[Expand]
Inherited Methods
From class org.springframework.context.support.GenericApplicationContext
From class org.springframework.context.support.AbstractApplicationContext
From class org.springframework.core.io.DefaultResourceLoader
From class java.lang.Object
From interface org.springframework.beans.factory.BeanFactory
From interface org.springframework.beans.factory.DisposableBean
From interface org.springframework.beans.factory.HierarchicalBeanFactory
From interface org.springframework.beans.factory.ListableBeanFactory
From interface org.springframework.beans.factory.support.BeanDefinitionRegistry
From interface org.springframework.context.ApplicationContext
From interface org.springframework.context.ApplicationEventPublisher
From interface org.springframework.context.ConfigurableApplicationContext
From interface org.springframework.context.Lifecycle
From interface org.springframework.context.MessageSource
From interface org.springframework.core.AliasRegistry
From interface org.springframework.core.env.EnvironmentCapable
From interface org.springframework.core.io.ResourceLoader
From interface org.springframework.core.io.support.ResourcePatternResolver

Public Constructors

public AnnotationConfigApplicationContext ()

Create a new AnnotationConfigApplicationContext that needs to be populated through register(Class...) calls and then manually refreshed.

public AnnotationConfigApplicationContext (Class...<?> annotatedClasses)

Create a new AnnotationConfigApplicationContext, deriving bean definitions from the given annotated classes and automatically refreshing the context.

Parameters
annotatedClasses one or more annotated classes, e.g. @Configuration classes

public AnnotationConfigApplicationContext (String... basePackages)

Create a new AnnotationConfigApplicationContext, scanning for bean definitions in the given packages and automatically refreshing the context.

Parameters
basePackages the packages to check for annotated classes

Public Methods

public void register (Class...<?> annotatedClasses)

Register an annotated class to be processed. Allows for programmatically building a AnnotationConfigApplicationContext. Note that refresh() must be called in order for the context to fully process the new class.

Calls to register(Class...) are idempotent; adding the same annotated class more than once has no additional effect.

Parameters
annotatedClasses one or more annotated classes, e.g. @Configuration classes
See Also

public void scan (String... basePackages)

Perform a scan within the specified base packages. Note that refresh() must be called in order for the context to fully process the new class.

Parameters
basePackages the packages to check for annotated classes
See Also

public void setBeanNameGenerator (BeanNameGenerator beanNameGenerator)

Set the BeanNameGenerator to use for detected bean classes.

Default is a AnnotationBeanNameGenerator.

public void setEnvironment (ConfigurableEnvironment environment)

Set the Environment for this application context.

Default value is determined by createEnvironment(). Replacing the default with this method is one option but configuration through getEnvironment() should also be considered. In either case, such modifications should be performed before refresh().

Delegates given environment to underlying AnnotatedBeanDefinitionReader and ClassPathBeanDefinitionScanner members.

Parameters
environment the new environment

public void setScopeMetadataResolver (ScopeMetadataResolver scopeMetadataResolver)

Set the ScopeMetadataResolver to use for detected bean classes.

The default is an AnnotationScopeMetadataResolver.