RootBeanDefinition class.
Implements the org.springframework.beans.factory.config.AutowireCapableBeanFactory
interface in addition to AbstractBeanFactory's createBean(java.lang.Class) method.
Provides bean creation (with constructor resolution), property population, wiring (including autowiring), and initialization. Handles runtime bean references, resolves managed collections, calls initialization methods, etc. Supports autowiring constructors, properties by name, and properties by type.
The main template method to be implemented by subclasses is
,
used for autowiring by type. In case of a factory which is capable of searching
its bean definitions, matching beans will typically be implemented through such
a search. For other factory styles, simplified matching algorithms can be implemented.
org.springframework.beans.factory.config.AutowireCapableBeanFactory.resolveDependency(org.springframework.beans.factory.config.DependencyDescriptor,java.lang.String,java.util.Set,org.springframework.beans.TypeConverter)
Note that this class does not assume or implement bean definition
registry capabilities. See for an implementation
of the DefaultListableBeanFactory and
org.springframework.beans.factory.ListableBeanFactory interfaces, which represent the API and SPI
view of such a factory, respectively.
BeanDefinitionRegistry
RootBeanDefinitionDefaultListableBeanFactoryBeanDefinitionRegistryprivate final Map factoryBeanInstanceCache = CollectionFactory.createConcurrentMapIfPossible(16);
Note that circular reference resolution means that one of the involved beans will receive a reference to another bean that is not fully initialized yet. This can lead to subtle and not-so-subtle side effects on initialization; it does work fine for many scenarios, though.
Default is "true". Turn this off to throw an exception when encountering a circular reference, disallowing them completely.
NOTE: It is generally recommended to not rely on circular references between your beans. Refactor your application logic to have the two beans involved delegate to a third bean that encapsulates their common logic.
This will only be used as a last resort in case of a circular reference that cannot be resolved otherwise: essentially, preferring a raw instance getting injected over a failure of the entire bean wiring process.
Default is "false", as of Spring 2.0. Turn this on to allow for non-wrapped raw beans injected into some of your references, which was Spring 1.2's (arguably unclean) default behavior.
NOTE: It is generally recommended to not rely on circular references between your beans, in particular with auto-proxying involved.
setAllowCircularReferences(boolean)This will typically be used by application contexts to register dependencies that are resolved in other ways, like BeanFactory through BeanFactoryAware or ApplicationContext through ApplicationContextAware.
By default, only the BeanFactoryAware interface is ignored. For further types to ignore, invoke this method for each type.
org.springframework.beans.factory.BeanFactoryAwareorg.springframework.context.ApplicationContextAwarepublic ObjectresolveDependency(DependencyDescriptor descriptor, String beanName) throws BeansException {
public ObjectcreateBean(Class beanClass, int autowireMode, boolean dependencyCheck) throws BeansException {
public Objectautowire(Class beanClass, int autowireMode, boolean dependencyCheck) throws BeansException {
public voidautowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
throw new IllegalArgumentException("AUTOWIRE_CONSTRUCTOR not supported for existing bean instance");
public voidapplyBeanPropertyValues(Object existingBean, String beanName) throws BeansException {
protected ObjectcreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
postProcessBeforeInstantiation callbacks.
Differentiates between default bean instantiation, use of a factory method, and autowiring a constructor.
beanName the name of the beanmbd the merged bean definition for the beanargs arguments to use if creating a prototype using explicit arguments to a
static factory method. This parameter must be null except in this case.org.springframework.beans.factory.BeanCreationException if the bean could not be createdinstantiateBean(java.lang.String,org.springframework.beans.factory.support.RootBeanDefinition)instantiateUsingFactoryMethod(java.lang.String,org.springframework.beans.factory.support.RootBeanDefinition,java.lang.Object[])autowireConstructor(java.lang.String,org.springframework.beans.factory.support.RootBeanDefinition,java.lang.reflect.Constructor[],java.lang.Object[])protected ObjectdoCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
protected ClasspredictBeanType(String beanName, RootBeanDefinition mbd, Class[] typesToMatch) {
This implementation determines the type matching 's
different creation strategies. As far as possible, we'll perform static
type checking to avoid creation of the target bean.
createBean(java.lang.Class)
beanName the name of the bean (for error handling purposes)mbd the merged bean definition for the beantypesToMatch the types to match in case of internal type matching purposes
(also signals that the returned Class will never be exposed to application code)null elsecreateBean(java.lang.Class)protected ClassgetTypeForFactoryMethod(String beanName, RootBeanDefinition mbd, Class[] typesToMatch) {
getObjectType method
on a plain instance of the FactoryBean, without bean properties applied yet.
If this doesn't return a type yet, a full creation of the FactoryBean is
used as fallback (through delegation to the superclass's implementation).
The shortcut check for a FactoryBean is only applied in case of a singleton FactoryBean. If the FactoryBean instance itself is not kept as singleton, it will be fully created to check the type of its exposed object.
beanName the name of the bean (for error handling purposes)mbd the merged bean definition for the beanbean the raw bean instancegetObjectType() call, without full initialization
of the FactoryBean.
beanName the name of the beanmbd the bean definition for the beannull to indicate
that we couldn't obtain a shortcut FactoryBean instanceprivate FactoryBeangetSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
getObjectType() call, without full initialization
of the FactoryBean.
beanName the name of the beanmbd the bean definition for the beannull to indicate
that we couldn't obtain a shortcut FactoryBean instanceprivate FactoryBeangetNonSingletonFactoryBeanForTypeCheck(String beanName, RootBeanDefinition mbd) {
postProcessMergedBeanDefinition methods.
mbd the merged bean definition for the beanbeanType the actual type of the managed bean instancebeanName the name of the beanorg.springframework.beans.BeansException if any post-processing failedMergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition,java.lang.Class,java.lang.String)protected voidapplyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class beanType, String beanName)
postProcessBeforeInstantiation methods.
Any returned object will be used as the bean instead of actually instantiating
the target bean. A null return value from the post-processor will
result in the target bean being instantiated.
beanClass the class of the bean to be instantiatedbeanName the name of the beannullorg.springframework.beans.BeansException if any post-processing failedorg.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class,java.lang.String)beanName the name of the beanmbd the bean definition for the beanargs arguments to use if creating a prototype using explicit arguments to a
static factory method. It is invalid to use a non-null args value in any other case.instantiateUsingFactoryMethod(java.lang.String,org.springframework.beans.factory.support.RootBeanDefinition,java.lang.Object[])autowireConstructor(java.lang.String,org.springframework.beans.factory.support.RootBeanDefinition,java.lang.reflect.Constructor[],java.lang.Object[])instantiateBean(java.lang.String,org.springframework.beans.factory.support.RootBeanDefinition)protected BeanWrappercreateBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
SmartInstantiationAwareBeanPostProcessors.
beanClass the raw class of the beanbeanName the name of the beannull if none specifiedorg.springframework.beans.BeansException in case of errorsorg.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor.determineCandidateConstructors(java.lang.Class,java.lang.String)protected Constructor[]determineConstructorsFromBeanPostProcessors(Class beanClass, String beanName)
SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) beanProcessor;
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
beanName the name of the beanmbd the bean definition for the beanexplicitArgs argument values passed in programmatically via the getBean method,
or null if none (-> use constructor argument values from bean definition)AbstractBeanFactory.getBean(java.lang.String,java.lang.Object[])This corresponds to constructor injection: In this mode, a Spring bean factory is able to host components that expect constructor-based dependency resolution.
beanName the name of the beanmbd the bean definition for the beanctors the chosen candidate constructorsexplicitArgs argument values passed in programmatically via the getBean method,
or null if none (-> use constructor argument values from bean definition)beanName the name of the bean we're wiring up.
Useful for debugging messages; not used functionally.mbd bean definition to update through autowiringbw BeanWrapper from which we can obtain information about the beanpvs the PropertyValues to register wired objects withThis is like PicoContainer default, in which there must be exactly one bean of the property type in the bean factory. This makes bean factories simple to configure for small namespaces, but doesn't work as well as standard Spring behavior for bigger applications.
beanName the name of the bean to autowire by typembd the merged bean definition to update through autowiringbw BeanWrapper from which we can obtain information about the beanpvs the PropertyValues to register wired objects withthrow new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, propertyName, ex);
mbd the merged bean definition the bean was created withbw the BeanWrapper the bean was created withorg.springframework.beans.BeanUtils.isSimpleProperty(java.lang.Class)protected String[]unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
bw the BeanWrapper the bean was created withisExcludedFromDependencyCheck(java.beans.PropertyDescriptor)This implementation excludes properties defined by CGLIB and properties whose type matches an ignored dependency type or which are defined by an ignored dependency interface.
pd the PropertyDescriptor of the bean propertyignoreDependencyType(java.lang.Class)ignoreDependencyInterface(java.lang.Class)beanName the name of the beanmbd the merged bean definition the bean was created withpds the relevant property descriptors for the target beanpvs the property values to be applied to the beanisExcludedFromDependencyCheck(java.beans.PropertyDescriptor)beanName the bean name passed for better exception informationmbd the merged bean definitionbw the BeanWrapper wrapping the target objectpvs the new property valuesprotected voidapplyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
private ObjectconvertForProperty(Object value, String propertyName, BeanWrapper bw, TypeConverter converter) {
Called from for traditionally defined beans,
and from createBean(java.lang.Class) for existing bean instances.
initializeBean(java.lang.Object,java.lang.String)
beanName the bean name in the factory (for debugging purposes)bean the new bean instance we may need to initializembd the bean definition that the bean was created with
(can also be null, if given an existing bean instance)org.springframework.beans.factory.BeanNameAwareorg.springframework.beans.factory.BeanClassLoaderAwareorg.springframework.beans.factory.BeanFactoryAwareapplyBeanPostProcessorsBeforeInitialization(java.lang.Object,java.lang.String)invokeInitMethods(java.lang.String,java.lang.Object,org.springframework.beans.factory.support.RootBeanDefinition)applyBeanPostProcessorsAfterInitialization(java.lang.Object,java.lang.String)beanName the bean name in the factory (for debugging purposes)bean the new bean instance we may need to initializembd the merged bean definition that the bean was created with
(can also be null, if given an existing bean instance)java.lang.Throwable if thrown by init methods or by the invocation processinvokeCustomInitMethod(java.lang.String,java.lang.Object,java.lang.String,boolean)if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
Can be overridden in subclasses for custom resolution of init methods with arguments.
beanName the bean name in the factory (for debugging purposes)bean the new bean instance we may need to initializeinitMethodName the name of the custom init methodenforceInitMethod indicates whether the defined init method needs to existinvokeInitMethods(java.lang.String,java.lang.Object,org.springframework.beans.factory.support.RootBeanDefinition)