Package org.springframework.context.annotation

# 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`, `org.springframework.beans.factory.BeanFactory`, `org.springframework.beans.factory.HierarchicalBeanFactory`, `org.springframework.beans.factory.ListableBeanFactory`, `org.springframework.beans.factory.support.BeanDefinitionRegistry`, `AnnotationConfigRegistry`, `ApplicationContext`, `ApplicationEventPublisher`, `ConfigurableApplicationContext`, `Lifecycle`, `MessageSource`, `org.springframework.core.AliasRegistry`, `org.springframework.core.env.EnvironmentCapable`, `org.springframework.core.io.ResourceLoader`, `org.springframework.core.io.support.ResourcePatternResolver`

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry
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.

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

`org.springframework.core.io.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, FACTORY_BEAN_PREFIX_CHAR`

### Fields inherited from interface org.springframework.context.ConfigurableApplicationContext

`APPLICATION_STARTUP_BEAN_NAME, BOOTSTRAP_EXECUTOR_BEAN_NAME, CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, ENVIRONMENT_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SHUTDOWN_HOOK_THREAD_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 through `register(java.lang.Class<?>...)` calls and then manually refreshed.
`AnnotationConfigApplicationContext(Class<?>... componentClasses)`
Create a new AnnotationConfigApplicationContext, deriving bean definitions from the given component classes and automatically refreshing the context.
`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.
`AnnotationConfigApplicationContext(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)`
Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.

## Method Summary

Modifier and Type
Method
Description
`void`
`register(Class<?>... componentClasses)`
Register one or more component classes to be processed.
`<T> void`
`registerBean(String beanName, Class<T> beanClass, Supplier<T> supplier, org.springframework.beans.factory.config.BeanDefinitionCustomizer... customizers)`
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).
`void`
`scan(String... basePackages)`
Perform a scan within the specified base packages.
`void`
`setBeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)`
Provide a custom `BeanNameGenerator` for use with `AnnotatedBeanDefinitionReader` and/or `ClassPathBeanDefinitionScanner`, if any.
`void`
`setEnvironment(org.springframework.core.env.ConfigurableEnvironment environment)`
Propagate the given custom `Environment` to the underlying `AnnotatedBeanDefinitionReader` and `ClassPathBeanDefinitionScanner`.
`void`
`setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)`
Set the `ScopeMetadataResolver` to use for registered component classes.

### Methods inherited from class org.springframework.context.support.GenericApplicationContext

`cancelRefresh, closeBeanFactory, getAutowireCapableBeanFactory, getBeanDefinition, getBeanFactory, getClassLoader, getDefaultListableBeanFactory, getResource, getResources, isAlias, isBeanDefinitionOverridable, isBeanNameInUse, refreshBeanFactory, refreshForAotProcessing, registerAlias, registerBean, registerBean, registerBean, registerBean, registerBean, registerBeanDefinition, removeAlias, removeBeanDefinition, setAllowBeanDefinitionOverriding, setAllowCircularReferences, setApplicationStartup, setClassLoader, setParent, setResourceLoader`

### Methods inherited from class org.springframework.context.support.AbstractApplicationContext

`addApplicationListener, addBeanFactoryPostProcessor, assertBeanFactoryActive, clearResourceCaches, close, containsBean, containsBeanDefinition, containsLocalBean, createEnvironment, destroyBeans, doClose, findAllAnnotationsOnBean, findAnnotationOnBean, findAnnotationOnBean, finishBeanFactoryInitialization, finishRefresh, getAliases, getApplicationListeners, getApplicationName, getApplicationStartup, getBean, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanProvider, getBeanProvider, getBeanProvider, getBeanProvider, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getEnvironment, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getResourcePatternResolver, getStartupDate, getType, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, initPropertySources, invokeBeanFactoryPostProcessors, isActive, isClosed, isPrototype, isRunning, isSingleton, isTypeMatch, isTypeMatch, obtainFreshBeanFactory, onClose, onRefresh, postProcessBeanFactory, prepareBeanFactory, prepareRefresh, publishEvent, publishEvent, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, removeApplicationListener, resetCommonCaches, setDisplayName, setId, start, stop, toString`

### Methods inherited from class org.springframework.core.io.DefaultResourceLoader

`addProtocolResolver, getProtocolResolvers, getResourceByPath, getResourceCache`

### 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 Details

### 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(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.

Parameters:
  `beanFactory` - the DefaultListableBeanFactory instance to use for this context

### AnnotationConfigApplicationContext

public AnnotationConfigApplicationContext(Class<?>... componentClasses)
Create a new AnnotationConfigApplicationContext, deriving bean definitions from the given component classes and automatically refreshing the context.

Parameters:
  `componentClasses` - one or more component classes — for example, `@Configuration` classes

### AnnotationConfigApplicationContext

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` - the packages to scan for component classes

## Method Details

### setEnvironment

public void setEnvironment(org.springframework.core.env.ConfigurableEnvironment environment)
Propagate the given custom `Environment` to the underlying `AnnotatedBeanDefinitionReader` and `ClassPathBeanDefinitionScanner`.

Specified by:
  `setEnvironment` in interface `ConfigurableApplicationContext`
Overrides:
  `setEnvironment` in class `AbstractApplicationContext`
Parameters:
  `environment` - the new environment
See Also:
  - `AbstractApplicationContext.createEnvironment()`

### setBeanNameGenerator

public void setBeanNameGenerator(org.springframework.beans.factory.support.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(org.springframework.beans.factory.support.BeanNameGenerator)`
  - `ClassPathBeanDefinitionScanner.setBeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator)`
  - `AnnotationBeanNameGenerator`
  - `FullyQualifiedAnnotationBeanNameGenerator`

### setScopeMetadataResolver

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...)`.

### register

public void register(Class<?>... componentClasses)
Register one or more component 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 interface `AnnotationConfigRegistry`
Parameters:
  `componentClasses` - one or more component classes — for example, `@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 interface `AnnotationConfigRegistry`
Parameters:
  `basePackages` - the packages to scan for component classes
See Also:
  - `register(Class...)`
  - `AbstractApplicationContext.refresh()`

### registerBean

public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier, org.springframework.beans.factory.config.BeanDefinitionCustomizer... customizers)
Description copied from class: `GenericApplicationContext`
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).

Overrides:
  `registerBean` in class `GenericApplicationContext`
Parameters:
  `beanName` - the name of the bean (may be `null`)
  `beanClass` - the class of the bean
  `supplier` - a callback for creating an instance of the bean (in case of `null`, resolving a public constructor to be autowired instead)
  `customizers` - one or more callbacks for customizing the factory's `BeanDefinition`, for example, setting a lazy-init or primary flag