Top Description Fields Constructors Methods
org.springframework.context.annotation

public Class AnnotationConfigApplicationContext

extends GenericApplicationContext
implements AnnotationConfigRegistry
Class Inheritance
All Implemented Interfaces
org.springframework.context.annotation.AnnotationConfigRegistry
Imports
java.util.Arrays, java.util.function.Supplier, org.springframework.beans.factory.config.BeanDefinitionCustomizer, org.springframework.beans.factory.support.BeanNameGenerator, .DefaultListableBeanFactory, org.springframework.context.support.GenericApplicationContext, org.springframework.core.env.ConfigurableEnvironment, org.springframework.core.metrics.StartupStep, org.springframework.lang.Nullable, org.springframework.util.Assert

Standalone application context, accepting component classes as input — in particular @Configuration-annotated classes, but also plain @Component types and JSR-330 compliant classes using jakarta.inject annotations.

Allows for registering classes one by one using register(Class...) as well as for classpath scanning using 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.

See @Configuration's javadoc for usage examples.

Authors
Juergen Hoeller, Chris Beams
Since
3.0
See Also
register, scan, AnnotatedBeanDefinitionReader, ClassPathBeanDefinitionScanner, org.springframework.context.support.GenericXmlApplicationContext

Field Summary

Modifier and TypeField and Description
private final AnnotatedBeanDefinitionReader
private final ClassPathBeanDefinitionScanner

Constructor Summary

AccessConstructor and Description
public
AnnotationConfigApplicationContext()

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

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

Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.

public
AnnotationConfigApplicationContext(Class<?>...
one or more component classes — for example, @Configuration classes
componentClasses
)

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

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

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

Method Summary

Modifier and TypeMethod and Description
public void
register(Class<?>...
one or more component classes — for example, @Configuration classes
componentClasses
)

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

Register one or more component classes to be processed.
public <T> void
registerBean(String
the name of the bean (may be null)
beanName
,
Class<T>
the class of the bean
beanClass
,
Supplier<T>
a callback for creating an instance of the bean (in case of null, resolving a public constructor to be autowired instead)
supplier
,
BeanDefinitionCustomizer...
one or more callbacks for customizing the factory's BeanDefinition, e.g. setting a lazy-init or primary flag
customizers
)

Overrides org.springframework.context.support.GenericApplicationContext.registerBean.

Register a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression).
public void
scan(String...
the packages to scan for component classes
basePackages
)

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

Perform a scan within the specified base packages.
public void
public void
public void
setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)

Set the ScopeMetadataResolver to use for registered component classes.

Inherited from org.springframework.context.support.GenericApplicationContext:
cancelRefreshcloseBeanFactorygetAutowireCapableBeanFactorygetBeanDefinitiongetBeanFactorygetClassLoadergetDefaultListableBeanFactorygetResourcegetResourcesisAliasisBeanDefinitionOverridableisBeanNameInUserefreshBeanFactoryrefreshForAotProcessingregisterAliasregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanregisterBeanDefinitionremoveAliasremoveBeanDefinitionsetAllowBeanDefinitionOverridingsetAllowCircularReferencessetApplicationStartupsetClassLoadersetParentsetResourceLoader

Field Detail

readerback to summary
private final AnnotatedBeanDefinitionReader reader
scannerback to summary
private final ClassPathBeanDefinitionScanner scanner

Constructor Detail

AnnotationConfigApplicationContextback to summary
public AnnotationConfigApplicationContext()

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

AnnotationConfigApplicationContextback to summary
public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory)

Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.

Parameters
beanFactory:DefaultListableBeanFactory

the DefaultListableBeanFactory instance to use for this context

AnnotationConfigApplicationContextback to summary
public AnnotationConfigApplicationContext(Class<?>... componentClasses)

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

Parameters
componentClasses:Class<?>[]

one or more component classes — for example, @Configuration classes

AnnotationConfigApplicationContextback to summary
public AnnotationConfigApplicationContext(String... basePackages)

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

Parameters
basePackages:String[]

the packages to scan for component classes

Method Detail

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 — for example, @Configuration classes

Annotations
@Override
See Also
scan(String...), refresh()
registerBeanback to summary
public <T> void registerBean(String beanName, Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers)

Overrides org.springframework.context.support.GenericApplicationContext.registerBean.

Doc from org.springframework.context.support.GenericApplicationContext.registerBean.

Register a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression).

This method can be overridden to adapt the registration mechanism for all registerBean methods (since they all delegate to this one).

Parameters
beanName:String

the name of the bean (may be null)

beanClass:Class<T>

the class of the bean

supplier:Supplier<T>

a callback for creating an instance of the bean (in case of null, resolving a public constructor to be autowired instead)

customizers:BeanDefinitionCustomizer[]

one or more callbacks for customizing the factory's BeanDefinition, e.g. setting a lazy-init or primary flag

Annotations
@Override
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 scan for component classes

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

Provide a custom BeanNameGenerator for use with AnnotatedBeanDefinitionReader and/or ClassPathBeanDefinitionScanner, if any.

Default is AnnotationBeanNameGenerator.

Any call to this method must occur prior to calls to register(Class...) and/or scan(String...).

See Also
AnnotatedBeanDefinitionReader#setBeanNameGenerator, ClassPathBeanDefinitionScanner#setBeanNameGenerator, AnnotationBeanNameGenerator, FullyQualifiedAnnotationBeanNameGenerator
setEnvironmentback to summary
public void setEnvironment(ConfigurableEnvironment environment)

Overrides org.springframework.context.support.AbstractApplicationContext.setEnvironment.

Implements org.springframework.context.ConfigurableApplicationContext.setEnvironment.

Propagate the given custom Environment to the underlying AnnotatedBeanDefinitionReader and ClassPathBeanDefinitionScanner.

Parameters
environment:ConfigurableEnvironment

Doc from org.springframework.context.ConfigurableApplicationContext.setEnvironment.

the new environment

Annotations
@Override
setScopeMetadataResolverback to summary
public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)

Set the ScopeMetadataResolver to use for registered component classes.

The default is an AnnotationScopeMetadataResolver.

Any call to this method must occur prior to calls to register(Class...) and/or scan(String...).