org.springframework.context.annotation
Class AnnotationConfigApplicationContext

java.lang.Object
  extended by org.springframework.core.io.DefaultResourceLoader
      extended by org.springframework.context.support.AbstractApplicationContext
          extended by org.springframework.context.support.GenericApplicationContext
              extended by org.springframework.context.annotation.AnnotationConfigApplicationContext
All Implemented Interfaces:
BeanFactory, DisposableBean, HierarchicalBeanFactory, ListableBeanFactory, BeanDefinitionRegistry, ApplicationContext, ApplicationEventPublisher, ConfigurableApplicationContext, Lifecycle, MessageSource, AliasRegistry, ResourceLoader, ResourcePatternResolver

public class AnnotationConfigApplicationContext
extends GenericApplicationContext

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(java.lang.Class...)) as well as for classpath scanning (scan(java.lang.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.

Since:
3.0
Author:
Chris Beams, Juergen Hoeller
See Also:
register(java.lang.Class...), scan(java.lang.String...), AnnotatedBeanDefinitionReader, ClassPathBeanDefinitionScanner, GenericXmlApplicationContext

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.context.ConfigurableApplicationContext
CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME
 
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
 
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
 
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
 
Constructor Summary
AnnotationConfigApplicationContext()
          Create a new AnnotationConfigApplicationContext that needs to be populated through register(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.
 
Method Summary
 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 setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
          Set the ScopeMetadataResolver to use for detected bean classes.
 
Methods inherited from class org.springframework.context.support.GenericApplicationContext
cancelRefresh, closeBeanFactory, getBeanDefinition, getBeanFactory, getDefaultListableBeanFactory, getResource, getResources, isAlias, isBeanNameInUse, refreshBeanFactory, registerAlias, registerBeanDefinition, removeAlias, removeBeanDefinition, setAllowBeanDefinitionOverriding, setAllowCircularReferences, setId, setParent, setResourceLoader
 
Methods inherited from class org.springframework.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, addListener, close, containsBean, containsBeanDefinition, containsLocalBean, destroy, destroyBeans, doClose, findAnnotationOnBean, finishBeanFactoryInitialization, finishRefresh, getAliases, getApplicationListeners, getAutowireCapableBeanFactory, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getResourcePatternResolver, getStartupDate, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, obtainFreshBeanFactory, onClose, onRefresh, postProcessBeanFactory, prepareBeanFactory, prepareRefresh, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, setDisplayName, start, stop, toString
 
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
getClassLoader, getResourceByPath, setClassLoader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry
containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames
 
Methods inherited from interface org.springframework.core.AliasRegistry
getAliases
 
Methods inherited from interface org.springframework.core.io.ResourceLoader
getClassLoader
 

Constructor Detail

AnnotationConfigApplicationContext

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


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

setBeanNameGenerator

public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Set the BeanNameGenerator to use for detected bean classes.

Default is a AnnotationBeanNameGenerator.


setScopeMetadataResolver

public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
Set the ScopeMetadataResolver to use for detected bean classes.

The default is an AnnotationScopeMetadataResolver.


register

public void register(Class<?>... annotatedClasses)
Register an annotated class to be processed. Allows for programmatically building a AnnotationConfigApplicationContext. Note that AbstractApplicationContext.refresh() must be called in order for the context to fully process the new class.

Calls to register(java.lang.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:
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 class.

Parameters:
basePackages - the packages to check for annotated classes
See Also:
AbstractApplicationContext.refresh()