public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry
@Configuration
-annotated classes, but also plain
@Component
types and JSR-330 compliant
classes using javax.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.
register(java.lang.Class<?>...)
,
scan(java.lang.String...)
,
AnnotatedBeanDefinitionReader
,
ClassPathBeanDefinitionScanner
,
GenericXmlApplicationContext
DefaultResourceLoader.ClassPathContextResource
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, LIFECYCLE_PROCESSOR_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME
CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, ENVIRONMENT_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME
FACTORY_BEAN_PREFIX
CLASSPATH_ALL_URL_PREFIX
CLASSPATH_URL_PREFIX
Constructor and Description |
---|
AnnotationConfigApplicationContext()
Create a new AnnotationConfigApplicationContext that needs to be populated
through
register(java.lang.Class<?>...) calls and then manually refreshed. |
AnnotationConfigApplicationContext(java.lang.Class<?>... annotatedClasses)
Create a new AnnotationConfigApplicationContext, deriving bean definitions
from the given annotated classes and automatically refreshing the context.
|
AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory)
Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.
|
AnnotationConfigApplicationContext(java.lang.String... basePackages)
Create a new AnnotationConfigApplicationContext, scanning for bean definitions
in the given packages and automatically refreshing the context.
|
Modifier and Type | Method and 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(java.lang.Class<?>... annotatedClasses)
Register one or more annotated classes to be processed.
|
void |
scan(java.lang.String... basePackages)
Perform a scan within the specified base packages.
|
void |
setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Provide a custom
BeanNameGenerator for use with AnnotatedBeanDefinitionReader
and/or ClassPathBeanDefinitionScanner , if any. |
void |
setEnvironment(ConfigurableEnvironment environment)
Set the
Environment for this application context. |
void |
setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
Set the
ScopeMetadataResolver to use for detected bean classes. |
cancelRefresh, closeBeanFactory, getAutowireCapableBeanFactory, getBeanDefinition, getBeanFactory, getClassLoader, getDefaultListableBeanFactory, getResource, getResources, isAlias, isBeanNameInUse, refreshBeanFactory, registerAlias, registerBeanDefinition, removeAlias, removeBeanDefinition, setAllowBeanDefinitionOverriding, setAllowCircularReferences, setClassLoader, setId, setParent, setResourceLoader
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, start, stop, toString
addProtocolResolver, getProtocolResolvers, getResourceByPath
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames
getAliases
addProtocolResolver
public AnnotationConfigApplicationContext()
register(java.lang.Class<?>...)
calls and then manually refreshed.public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory)
beanFactory
- the DefaultListableBeanFactory instance to use for this contextpublic AnnotationConfigApplicationContext(java.lang.Class<?>... annotatedClasses)
annotatedClasses
- one or more annotated classes,
e.g. @Configuration
classespublic AnnotationConfigApplicationContext(java.lang.String... basePackages)
basePackages
- the packages to check for annotated classespublic void setEnvironment(ConfigurableEnvironment environment)
Environment
for this application context.
Default value is determined by AbstractApplicationContext.createEnvironment()
. Replacing the
default with this method is one option but configuration through AbstractApplicationContext.getEnvironment()
should also be considered. In either case, such modifications
should be performed before AbstractApplicationContext.refresh()
.
Delegates given environment to underlying AnnotatedBeanDefinitionReader
and ClassPathBeanDefinitionScanner
members.
setEnvironment
in interface ConfigurableApplicationContext
setEnvironment
in class AbstractApplicationContext
environment
- the new environmentAbstractApplicationContext.createEnvironment()
public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
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...)
.
public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
ScopeMetadataResolver
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/or scan(String...)
.
protected void prepareRefresh()
AbstractApplicationContext
prepareRefresh
in class AbstractApplicationContext
public void register(java.lang.Class<?>... annotatedClasses)
Note that AbstractApplicationContext.refresh()
must be called in order for the context
to fully process the new classes.
register
in interface AnnotationConfigRegistry
annotatedClasses
- one or more annotated classes,
e.g. @Configuration
classesscan(String...)
,
AbstractApplicationContext.refresh()
public void scan(java.lang.String... basePackages)
Note that AbstractApplicationContext.refresh()
must be called in order for the context
to fully process the new classes.
scan
in interface AnnotationConfigRegistry
basePackages
- the packages to check for annotated classesregister(Class...)
,
AbstractApplicationContext.refresh()