Class AnnotationConfigApplicationContext
- java.lang.Object
- org.springframework.core.io.DefaultResourceLoader
- org.springframework.context.support.AbstractApplicationContext
- org.springframework.context.support.GenericApplicationContext
- org.springframework.context.annotation.AnnotationConfigApplicationContext
- All Implemented Interfaces:
Closeable
,AutoCloseable
,BeanFactory
,DisposableBean
,HierarchicalBeanFactory
,ListableBeanFactory
,BeanDefinitionRegistry
,AnnotationConfigRegistry
,ApplicationContext
,ApplicationEventPublisher
,ConfigurableApplicationContext
,Lifecycle
,MessageSource
,AliasRegistry
,EnvironmentCapable
,ResourceLoader
,ResourcePatternResolver
public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry
Standalone application context, accepting annotated classes as input - in particular@Configuration
-annotated classes, but also plain@Component
types and JSR-330 compliant classes usingjavax.inject
annotations. Allows for registering classes one by one usingregister(Class...)
as well as for classpath scanning usingscan(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.- Since:
- 3.0
- Author:
- Juergen Hoeller, Chris Beams
- See Also:
register(java.lang.Class<?>...)
,scan(java.lang.String...)
,AnnotatedBeanDefinitionReader
,ClassPathBeanDefinitionScanner
,GenericXmlApplicationContext
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.core.io.DefaultResourceLoader
DefaultResourceLoader.ClassPathContextResource
Field Summary
Fields inherited from class org.springframework.context.support.AbstractApplicationContext
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, LIFECYCLE_PROCESSOR_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
Fields inherited from interface org.springframework.context.ConfigurableApplicationContext
CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, ENVIRONMENT_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
Constructor Summary
Constructors Constructor Description AnnotationConfigApplicationContext()
Create a new AnnotationConfigApplicationContext that needs to be populated throughregister(java.lang.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.AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory)
Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
prepareRefresh()
Prepare this context for refreshing, setting its startup date and active flag as well as performing any initialization of property sources.void
register(Class<?>... annotatedClasses)
Register one or more annotated classes to be processed.void
scan(String... basePackages)
Perform a scan within the specified base packages.void
setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Provide a customBeanNameGenerator
for use withAnnotatedBeanDefinitionReader
and/orClassPathBeanDefinitionScanner
, if any.void
setEnvironment(ConfigurableEnvironment environment)
Propagates the given customEnvironment
to the underlyingAnnotatedBeanDefinitionReader
andClassPathBeanDefinitionScanner
.void
setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
Set theScopeMetadataResolver
to use for detected bean classes.Methods inherited from class org.springframework.context.support.GenericApplicationContext
cancelRefresh, closeBeanFactory, getAutowireCapableBeanFactory, getBeanDefinition, getBeanFactory, getClassLoader, getDefaultListableBeanFactory, getResource, getResources, isAlias, isBeanNameInUse, refreshBeanFactory, registerAlias, registerBeanDefinition, removeAlias, removeBeanDefinition, setAllowBeanDefinitionOverriding, setAllowCircularReferences, setClassLoader, setParent, setResourceLoader
Methods inherited from class org.springframework.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, assertBeanFactoryActive, close, containsBean, containsBeanDefinition, containsLocalBean, createEnvironment, destroy, destroyBeans, doClose, findAnnotationOnBean, finishBeanFactoryInitialization, finishRefresh, getAliases, getApplicationListeners, getApplicationName, getBean, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getEnvironment, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getResourcePatternResolver, getStartupDate, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, initPropertySources, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, isTypeMatch, obtainFreshBeanFactory, onClose, onRefresh, postProcessBeanFactory, prepareBeanFactory, publishEvent, publishEvent, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, resetCommonCaches, setDisplayName, setId, start, stop, toString
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
addProtocolResolver, getProtocolResolvers, getResourceByPath
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.core.AliasRegistry
getAliases
Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry
containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames
Methods inherited from interface org.springframework.context.ConfigurableApplicationContext
addProtocolResolver
Constructor Detail
AnnotationConfigApplicationContext
public AnnotationConfigApplicationContext()
Create a new AnnotationConfigApplicationContext that needs to be populated throughregister(java.lang.Class<?>...)
calls and then manually refreshed.
AnnotationConfigApplicationContext
public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory)
Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.- Parameters:
beanFactory
- the DefaultListableBeanFactory instance to use for this context
AnnotationConfigApplicationContext
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
AnnotationConfigApplicationContext
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
Method Detail
setEnvironment
public void setEnvironment(ConfigurableEnvironment environment)
Propagates the given customEnvironment
to the underlyingAnnotatedBeanDefinitionReader
andClassPathBeanDefinitionScanner
.- Specified by:
setEnvironment
in interfaceConfigurableApplicationContext
- Overrides:
setEnvironment
in classAbstractApplicationContext
- Parameters:
environment
- the new environment- See Also:
AbstractApplicationContext.createEnvironment()
setBeanNameGenerator
public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Provide a customBeanNameGenerator
for use withAnnotatedBeanDefinitionReader
and/orClassPathBeanDefinitionScanner
, if any.Default is
AnnotationBeanNameGenerator
.Any call to this method must occur prior to calls to
register(Class...)
and/orscan(String...)
.
setScopeMetadataResolver
public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
Set theScopeMetadataResolver
to use for detected bean classes.The default is an
AnnotationScopeMetadataResolver
.Any call to this method must occur prior to calls to
register(Class...)
and/orscan(String...)
.
prepareRefresh
protected void prepareRefresh()
Description copied from class:AbstractApplicationContext
Prepare this context for refreshing, setting its startup date and active flag as well as performing any initialization of property sources.- Overrides:
prepareRefresh
in classAbstractApplicationContext
register
public void register(Class<?>... annotatedClasses)
Register one or more annotated classes to be processed.Note that
AbstractApplicationContext.refresh()
must be called in order for the context to fully process the new classes.- Specified by:
register
in interfaceAnnotationConfigRegistry
- Parameters:
annotatedClasses
- one or more annotated classes, e.g.@Configuration
classes- See Also:
scan(String...)
,AbstractApplicationContext.refresh()
scan
public void scan(String... basePackages)
Perform a scan within the specified base packages.Note that
AbstractApplicationContext.refresh()
must be called in order for the context to fully process the new classes.- Specified by:
scan
in interfaceAnnotationConfigRegistry
- Parameters:
basePackages
- the packages to check for annotated classes- See Also:
register(Class...)
,AbstractApplicationContext.refresh()