public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, java.io.Serializable
ListableBeanFactory and
BeanDefinitionRegistry interfaces: a full-fledged bean factory
based on bean definition objects.
Typical usage is registering all bean definitions first (possibly read from a bean definition file), before accessing beans. Bean definition lookup is therefore an inexpensive operation in a local bean definition table, operating on pre-built bean definition metadata objects.
Can be used as a standalone bean factory, or as a superclass for custom
bean factories. Note that readers for specific bean definition formats are
typically implemented separately rather than as bean factory subclasses:
see for example PropertiesBeanDefinitionReader and
XmlBeanDefinitionReader.
For an alternative implementation of the
ListableBeanFactory interface,
have a look at StaticListableBeanFactory, which manages existing
bean instances rather than creating new ones based on bean definitions.
StaticListableBeanFactory,
PropertiesBeanDefinitionReader,
XmlBeanDefinitionReader,
Serialized Formlogger, NULL_OBJECTAUTOWIRE_AUTODETECT, AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_CONSTRUCTOR, AUTOWIRE_NOSCOPE_PROTOTYPE, SCOPE_SINGLETONFACTORY_BEAN_PREFIX| Constructor and Description |
|---|
DefaultListableBeanFactory()
Create a new DefaultListableBeanFactory.
|
DefaultListableBeanFactory(BeanFactory parentBeanFactory)
Create a new DefaultListableBeanFactory with the given parent.
|
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
allowAliasOverriding()
Only allows alias overriding if bean definition overriding is allowed.
|
void |
clearMetadataCache()
Clear the merged bean definition cache, removing entries for beans
which are not considered eligible for full metadata caching yet.
|
boolean |
containsBeanDefinition(java.lang.String beanName)
Check if this bean factory contains a bean definition with the given name.
|
void |
copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
Copy all relevant configuration from the given other factory.
|
void |
destroySingleton(java.lang.String beanName)
Destroy the given bean.
|
void |
destroySingletons()
Destroy all singleton beans in this factory, including inner beans that have
been registered as disposable.
|
protected java.lang.String |
determineAutowireCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
DependencyDescriptor descriptor)
Determine the autowire candidate in the given set of beans.
|
protected java.lang.String |
determineHighestPriorityCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
java.lang.Class<?> requiredType)
Determine the candidate with the highest priority in the given set of beans.
|
protected java.lang.String |
determinePrimaryCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
java.lang.Class<?> requiredType)
Determine the primary candidate in the given set of beans.
|
java.lang.Object |
doResolveDependency(DependencyDescriptor descriptor,
java.lang.String beanName,
java.util.Set<java.lang.String> autowiredBeanNames,
TypeConverter typeConverter) |
<A extends java.lang.annotation.Annotation> |
findAnnotationOnBean(java.lang.String beanName,
java.lang.Class<A> annotationType)
Find a
Annotation of annotationType on the specified
bean, traversing its interfaces and super classes if no annotation can be
found on the given class itself, as well as checking its raw bean class
if not found on the exposed bean reference (e.g. |
protected java.util.Map<java.lang.String,java.lang.Object> |
findAutowireCandidates(java.lang.String beanName,
java.lang.Class<?> requiredType,
DependencyDescriptor descriptor)
Find bean instances that match the required type.
|
void |
freezeConfiguration()
Freeze all bean definitions, signalling that the registered bean definitions
will not be modified or post-processed any further.
|
AutowireCandidateResolver |
getAutowireCandidateResolver()
Return the autowire candidate resolver for this BeanFactory (never
null). |
<T> T |
getBean(java.lang.Class<T> requiredType)
Return the bean instance that uniquely matches the given object type, if any.
|
<T> T |
getBean(java.lang.Class<T> requiredType,
java.lang.Object... args)
Return an instance, which may be shared or independent, of the specified bean.
|
BeanDefinition |
getBeanDefinition(java.lang.String beanName)
Return the bean definition for the given bean name.
|
int |
getBeanDefinitionCount()
Return the number of beans defined in the factory.
|
java.lang.String[] |
getBeanDefinitionNames()
Return the names of all beans defined in this factory.
|
java.lang.String[] |
getBeanNamesForAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Find all names of beans whose
Class has the supplied Annotation
type, without creating any bean instances yet. |
java.lang.String[] |
getBeanNamesForType(java.lang.Class<?> type)
Return the names of beans matching the given type (including subclasses),
judging from either bean definitions or the value of
getObjectType
in the case of FactoryBeans. |
java.lang.String[] |
getBeanNamesForType(java.lang.Class<?> type,
boolean includeNonSingletons,
boolean allowEagerInit)
Return the names of beans matching the given type (including subclasses),
judging from either bean definitions or the value of
getObjectType
in the case of FactoryBeans. |
java.lang.String[] |
getBeanNamesForType(org.springframework.core.ResolvableType type)
Return the names of beans matching the given type (including subclasses),
judging from either bean definitions or the value of
getObjectType
in the case of FactoryBeans. |
java.util.Iterator<java.lang.String> |
getBeanNamesIterator()
Return a unified view over all bean names managed by this factory.
|
<T> java.util.Map<java.lang.String,T> |
getBeansOfType(java.lang.Class<T> type)
Return the bean instances that match the given object type (including
subclasses), judging from either bean definitions or the value of
getObjectType in the case of FactoryBeans. |
<T> java.util.Map<java.lang.String,T> |
getBeansOfType(java.lang.Class<T> type,
boolean includeNonSingletons,
boolean allowEagerInit)
Return the bean instances that match the given object type (including
subclasses), judging from either bean definitions or the value of
getObjectType in the case of FactoryBeans. |
java.util.Map<java.lang.String,java.lang.Object> |
getBeansWithAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Find all beans whose
Class has the supplied Annotation type,
returning a Map of bean names with corresponding bean instances. |
java.util.Comparator<java.lang.Object> |
getDependencyComparator()
Return the dependency comparator for this BeanFactory (may be
null. |
protected java.lang.Integer |
getPriority(java.lang.Object beanInstance)
Return the priority assigned for the given bean instance by
the
javax.annotation.Priority annotation. |
java.lang.String |
getSerializationId()
Return an id for serialization purposes, if specified, allowing this BeanFactory
to be deserialized from this id back into the BeanFactory object, if needed.
|
boolean |
isAllowBeanDefinitionOverriding()
Return whether it should be allowed to override bean definitions by registering
a different definition with the same name, automatically replacing the former.
|
boolean |
isAllowEagerClassLoading()
Return whether the factory is allowed to eagerly load bean classes
even for bean definitions that are marked as "lazy-init".
|
boolean |
isAutowireCandidate(java.lang.String beanName,
DependencyDescriptor descriptor)
Determine whether the specified bean qualifies as an autowire candidate,
to be injected into other beans which declare a dependency of matching type.
|
protected boolean |
isAutowireCandidate(java.lang.String beanName,
DependencyDescriptor descriptor,
AutowireCandidateResolver resolver)
Determine whether the specified bean definition qualifies as an autowire candidate,
to be injected into other beans which declare a dependency of matching type.
|
protected boolean |
isAutowireCandidate(java.lang.String beanName,
RootBeanDefinition mbd,
DependencyDescriptor descriptor,
AutowireCandidateResolver resolver)
Determine whether the specified bean definition qualifies as an autowire candidate,
to be injected into other beans which declare a dependency of matching type.
|
protected boolean |
isBeanEligibleForMetadataCaching(java.lang.String beanName)
Considers all beans as eligible for metadata caching
if the factory's configuration has been marked as frozen.
|
boolean |
isConfigurationFrozen()
Return whether this factory's bean definitions are frozen,
i.e.
|
protected boolean |
isPrimary(java.lang.String beanName,
java.lang.Object beanInstance)
Return whether the bean definition for the given bean name has been
marked as a primary bean.
|
protected boolean |
matchesBeanName(java.lang.String beanName,
java.lang.String candidateName)
Determine whether the given candidate name matches the bean name or the aliases
stored in this bean definition.
|
void |
preInstantiateSingletons()
Ensure that all non-lazy-init singletons are instantiated, also considering
FactoryBeans. |
void |
registerBeanDefinition(java.lang.String beanName,
BeanDefinition beanDefinition)
Register a new bean definition with this registry.
|
void |
registerResolvableDependency(java.lang.Class<?> dependencyType,
java.lang.Object autowiredValue)
Register a special dependency type with corresponding autowired value.
|
void |
registerSingleton(java.lang.String beanName,
java.lang.Object singletonObject)
Register the given existing object as singleton in the bean registry,
under the given bean name.
|
void |
removeBeanDefinition(java.lang.String beanName)
Remove the BeanDefinition for the given name.
|
protected void |
resetBeanDefinition(java.lang.String beanName)
Reset all bean definition caches for the given bean,
including the caches of beans that are derived from it.
|
java.lang.Object |
resolveDependency(DependencyDescriptor descriptor,
java.lang.String requestingBeanName,
java.util.Set<java.lang.String> autowiredBeanNames,
TypeConverter typeConverter)
Resolve the specified dependency against the beans defined in this factory.
|
<T> NamedBeanHolder<T> |
resolveNamedBean(java.lang.Class<T> requiredType)
Resolve the bean instance that uniquely matches the given object type, if any,
including its bean name.
|
void |
setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
Set whether it should be allowed to override bean definitions by registering
a different definition with the same name, automatically replacing the former.
|
void |
setAllowEagerClassLoading(boolean allowEagerClassLoading)
Set whether the factory is allowed to eagerly load bean classes
even for bean definitions that are marked as "lazy-init".
|
void |
setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver)
Set a custom autowire candidate resolver for this BeanFactory to use
when deciding whether a bean definition should be considered as a
candidate for autowiring.
|
void |
setDependencyComparator(java.util.Comparator<java.lang.Object> dependencyComparator)
Set a
Comparator for dependency Lists and arrays. |
void |
setSerializationId(java.lang.String serializationId)
Specify an id for serialization purposes, allowing this BeanFactory to be
deserialized from this id back into the BeanFactory object, if needed.
|
java.lang.String |
toString() |
protected java.lang.Object |
writeReplace() |
applyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPostProcessorsBeforeInstantiation, applyBeanPropertyValues, applyMergedBeanDefinitionPostProcessors, applyPropertyValues, autowire, autowireBean, autowireBeanProperties, autowireByName, autowireByType, autowireConstructor, checkDependencies, clearSingletonCache, configureBean, createBean, createBean, createBean, createBeanInstance, destroyBean, determineConstructorsFromBeanPostProcessors, determineTargetType, doCreateBean, filterPropertyDescriptorsForDependencyCheck, filterPropertyDescriptorsForDependencyCheck, getEarlyBeanReference, getInstantiationStrategy, getParameterNameDiscoverer, getTypeForFactoryBean, getTypeForFactoryMethod, ignoreDependencyInterface, ignoreDependencyType, initializeBean, initializeBean, instantiateBean, instantiateUsingFactoryMethod, invokeCustomInitMethod, invokeInitMethods, isExcludedFromDependencyCheck, populateBean, postProcessObjectFromFactoryBean, predictBeanType, removeSingleton, resolveBeforeInstantiation, resolveDependency, setAllowCircularReferences, setAllowRawInjectionDespiteWrapping, setInstantiationStrategy, setParameterNameDiscoverer, unsatisfiedNonSimplePropertiesaddBeanPostProcessor, addEmbeddedValueResolver, addPropertyEditorRegistrar, afterPrototypeCreation, beforePrototypeCreation, checkMergedBeanDefinition, cleanupAfterBeanCreationFailure, clearMergedBeanDefinition, containsBean, containsLocalBean, copyRegisteredEditorsTo, destroyBean, destroyBean, destroyScopedBean, doGetBean, evaluateBeanDefinitionString, getAccessControlContext, getAliases, getBean, getBean, getBean, getBean, getBeanClassLoader, getBeanExpressionResolver, getBeanPostProcessorCount, getBeanPostProcessors, getConversionService, getCustomEditors, getCustomTypeConverter, getMergedBeanDefinition, getMergedBeanDefinition, getMergedBeanDefinition, getMergedLocalBeanDefinition, getObjectForBeanInstance, getParentBeanFactory, getPropertyEditorRegistrars, getRegisteredScope, getRegisteredScopeNames, getTempClassLoader, getType, getTypeConverter, hasBeanCreationStarted, hasDestructionAwareBeanPostProcessors, hasEmbeddedValueResolver, hasInstantiationAwareBeanPostProcessors, initBeanWrapper, isActuallyInCreation, isBeanNameInUse, isCacheBeanMetadata, isFactoryBean, isFactoryBean, isPrototype, isPrototypeCurrentlyInCreation, isSingleton, isTypeMatch, isTypeMatch, markBeanAsCreated, originalBeanName, registerCustomEditor, registerCustomEditors, registerDisposableBeanIfNecessary, registerScope, removeSingletonIfCreatedForTypeCheckOnly, requiresDestruction, resolveBeanClass, resolveEmbeddedValue, setBeanClassLoader, setBeanExpressionResolver, setCacheBeanMetadata, setConversionService, setParentBeanFactory, setSecurityContextProvider, setTempClassLoader, setTypeConverter, transformedBeanNamegetCachedObjectForFactoryBean, getFactoryBean, getObjectFromFactoryBean, getTypeForFactoryBeanaddSingleton, addSingletonFactory, afterSingletonCreation, beforeSingletonCreation, containsSingleton, destroyBean, getDependenciesForBean, getDependentBeans, getSingleton, getSingleton, getSingleton, getSingletonCount, getSingletonMutex, getSingletonNames, hasDependentBean, isCurrentlyInCreation, isDependent, isSingletonCurrentlyInCreation, onSuppressedException, registerContainedBean, registerDependentBean, registerDisposableBean, setCurrentlyInCreationcanonicalName, checkForAliasCircle, hasAlias, isAlias, registerAlias, removeAlias, resolveAliasesclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitignoreDependencyInterface, ignoreDependencyTypeapplyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPropertyValues, autowire, autowireBean, autowireBeanProperties, configureBean, createBean, createBean, destroyBean, initializeBean, resolveDependencyaddBeanPostProcessor, addEmbeddedValueResolver, addPropertyEditorRegistrar, copyRegisteredEditorsTo, destroyBean, destroyScopedBean, getAccessControlContext, getBeanClassLoader, getBeanExpressionResolver, getBeanPostProcessorCount, getConversionService, getDependenciesForBean, getDependentBeans, getMergedBeanDefinition, getRegisteredScope, getRegisteredScopeNames, getTempClassLoader, getTypeConverter, hasEmbeddedValueResolver, isCacheBeanMetadata, isCurrentlyInCreation, isFactoryBean, registerAlias, registerCustomEditor, registerDependentBean, registerScope, resolveAliases, resolveEmbeddedValue, setBeanClassLoader, setBeanExpressionResolver, setCacheBeanMetadata, setConversionService, setCurrentlyInCreation, setParentBeanFactory, setTempClassLoader, setTypeConvertercontainsLocalBean, getParentBeanFactorycontainsBean, getAliases, getBean, getBean, getBean, getType, isPrototype, isSingleton, isTypeMatch, isTypeMatchcontainsSingleton, getSingleton, getSingletonCount, getSingletonMutex, getSingletonNamesisBeanNameInUsepublic DefaultListableBeanFactory()
public DefaultListableBeanFactory(BeanFactory parentBeanFactory)
parentBeanFactory - the parent BeanFactorypublic void setSerializationId(java.lang.String serializationId)
public java.lang.String getSerializationId()
public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
Default is "true".
public boolean isAllowBeanDefinitionOverriding()
public void setAllowEagerClassLoading(boolean allowEagerClassLoading)
Default is "true". Turn this flag off to suppress class loading for lazy-init beans unless such a bean is explicitly requested. In particular, by-type lookups will then simply ignore bean definitions without resolved class name, instead of loading the bean classes on demand just to perform a type check.
public boolean isAllowEagerClassLoading()
public void setDependencyComparator(java.util.Comparator<java.lang.Object> dependencyComparator)
Comparator for dependency Lists and arrays.OrderComparator,
AnnotationAwareOrderComparatorpublic java.util.Comparator<java.lang.Object> getDependencyComparator()
null.public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver)
public AutowireCandidateResolver getAutowireCandidateResolver()
null).public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)
ConfigurableBeanFactoryShould include all standard configuration settings as well as BeanPostProcessors, Scopes, and factory-specific internal settings. Should not include any metadata of actual bean definitions, such as BeanDefinition objects and bean name aliases.
copyConfigurationFrom in interface ConfigurableBeanFactorycopyConfigurationFrom in class AbstractAutowireCapableBeanFactoryotherFactory - the other BeanFactory to copy frompublic <T> T getBean(java.lang.Class<T> requiredType)
throws BeansException
BeanFactoryThis method goes into ListableBeanFactory by-type lookup territory
but may also be translated into a conventional by-name lookup based on the name
of the given type. For more extensive retrieval operations across sets of beans,
use ListableBeanFactory and/or BeanFactoryUtils.
getBean in interface BeanFactoryrequiredType - type the bean must match; can be an interface or superclass.
null is disallowed.NoSuchBeanDefinitionException - if no bean of the given type was foundNoUniqueBeanDefinitionException - if more than one bean of the given type was foundBeansException - if the bean could not be createdListableBeanFactorypublic <T> T getBean(java.lang.Class<T> requiredType,
java.lang.Object... args)
throws BeansException
BeanFactoryAllows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.
This method goes into ListableBeanFactory by-type lookup territory
but may also be translated into a conventional by-name lookup based on the name
of the given type. For more extensive retrieval operations across sets of beans,
use ListableBeanFactory and/or BeanFactoryUtils.
getBean in interface BeanFactoryrequiredType - type the bean must match; can be an interface or superclass.
null is disallowed.args - arguments to use when creating a bean instance using explicit arguments
(only applied when creating a new instance as opposed to retrieving an existing one)NoSuchBeanDefinitionException - if there is no such bean definitionBeanDefinitionStoreException - if arguments have been given but
the affected bean isn't a prototypeBeansException - if the bean could not be createdpublic boolean containsBeanDefinition(java.lang.String beanName)
AbstractBeanFactorycontainsBean when no cached singleton instance is found.
Depending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.
containsBeanDefinition in interface ListableBeanFactorycontainsBeanDefinition in interface BeanDefinitionRegistrycontainsBeanDefinition in class AbstractBeanFactorybeanName - the name of the bean to look forAbstractBeanFactory.containsBean(java.lang.String),
ListableBeanFactory.containsBeanDefinition(java.lang.String)public int getBeanDefinitionCount()
ListableBeanFactoryDoes not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionCount in interface ListableBeanFactorygetBeanDefinitionCount in interface BeanDefinitionRegistrypublic java.lang.String[] getBeanDefinitionNames()
ListableBeanFactoryDoes not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionNames in interface ListableBeanFactorygetBeanDefinitionNames in interface BeanDefinitionRegistrypublic java.lang.String[] getBeanNamesForType(org.springframework.core.ResolvableType type)
ListableBeanFactorygetObjectType
in the case of FactoryBeans.
NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.
Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
This version of getBeanNamesForType matches all kinds of beans,
be it singletons, prototypes, or FactoryBeans. In most implementations, the
result will be the same as for getBeanNamesForType(type, true, true).
Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.
getBeanNamesForType in interface ListableBeanFactorytype - the class or interface to match, or null for all bean namesBeanFactory.isTypeMatch(String, ResolvableType),
FactoryBean.getObjectType(),
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)public java.lang.String[] getBeanNamesForType(java.lang.Class<?> type)
ListableBeanFactorygetObjectType
in the case of FactoryBeans.
NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.
Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
This version of getBeanNamesForType matches all kinds of beans,
be it singletons, prototypes, or FactoryBeans. In most implementations, the
result will be the same as for getBeanNamesForType(type, true, true).
Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.
getBeanNamesForType in interface ListableBeanFactorytype - the class or interface to match, or null for all bean namesFactoryBean.getObjectType(),
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)public java.lang.String[] getBeanNamesForType(java.lang.Class<?> type,
boolean includeNonSingletons,
boolean allowEagerInit)
ListableBeanFactorygetObjectType
in the case of FactoryBeans.
NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.
Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesForTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.
getBeanNamesForType in interface ListableBeanFactorytype - the class or interface to match, or null for all bean namesincludeNonSingletons - whether to include prototype or scoped beans too
or just singletons (also applies to FactoryBeans)allowEagerInit - whether to initialize lazy-init singletons and
objects created by FactoryBeans (or by factory methods with a
"factory-bean" reference) for the type check. Note that FactoryBeans need to be
eagerly initialized to determine their type: So be aware that passing in "true"
for this flag will initialize FactoryBeans and "factory-bean" references.FactoryBean.getObjectType(),
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)public <T> java.util.Map<java.lang.String,T> getBeansOfType(java.lang.Class<T> type)
throws BeansException
ListableBeanFactorygetObjectType in the case of FactoryBeans.
NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.
Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beansOfTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
This version of getBeansOfType matches all kinds of beans, be it
singletons, prototypes, or FactoryBeans. In most implementations, the
result will be the same as for getBeansOfType(type, true, true).
The Map returned by this method should always return bean names and corresponding bean instances in the order of definition in the backend configuration, as far as possible.
getBeansOfType in interface ListableBeanFactorytype - the class or interface to match, or null for all concrete beansBeansException - if a bean could not be createdFactoryBean.getObjectType(),
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)public <T> java.util.Map<java.lang.String,T> getBeansOfType(java.lang.Class<T> type,
boolean includeNonSingletons,
boolean allowEagerInit)
throws BeansException
ListableBeanFactorygetObjectType in the case of FactoryBeans.
NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well.
Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beansOfTypeIncludingAncestors
to include beans in ancestor factories too.
Note: Does not ignore singleton beans that have been registered by other means than bean definitions.
The Map returned by this method should always return bean names and corresponding bean instances in the order of definition in the backend configuration, as far as possible.
getBeansOfType in interface ListableBeanFactorytype - the class or interface to match, or null for all concrete beansincludeNonSingletons - whether to include prototype or scoped beans too
or just singletons (also applies to FactoryBeans)allowEagerInit - whether to initialize lazy-init singletons and
objects created by FactoryBeans (or by factory methods with a
"factory-bean" reference) for the type check. Note that FactoryBeans need to be
eagerly initialized to determine their type: So be aware that passing in "true"
for this flag will initialize FactoryBeans and "factory-bean" references.BeansException - if a bean could not be createdFactoryBean.getObjectType(),
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)public java.lang.String[] getBeanNamesForAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
ListableBeanFactoryClass has the supplied Annotation
type, without creating any bean instances yet.getBeanNamesForAnnotation in interface ListableBeanFactoryannotationType - the type of annotation to look forpublic java.util.Map<java.lang.String,java.lang.Object> getBeansWithAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
ListableBeanFactoryClass has the supplied Annotation type,
returning a Map of bean names with corresponding bean instances.getBeansWithAnnotation in interface ListableBeanFactoryannotationType - the type of annotation to look forpublic <A extends java.lang.annotation.Annotation> A findAnnotationOnBean(java.lang.String beanName,
java.lang.Class<A> annotationType)
throws NoSuchBeanDefinitionException
Annotation of annotationType on the specified
bean, traversing its interfaces and super classes if no annotation can be
found on the given class itself, as well as checking its raw bean class
if not found on the exposed bean reference (e.g. in case of a proxy).findAnnotationOnBean in interface ListableBeanFactorybeanName - the name of the bean to look for annotations onannotationType - the annotation class to look fornullNoSuchBeanDefinitionException - if there is no bean with the given namepublic void registerResolvableDependency(java.lang.Class<?> dependencyType,
java.lang.Object autowiredValue)
ConfigurableListableBeanFactoryThis is intended for factory/context references that are supposed to be autowirable but are not defined as beans in the factory: e.g. a dependency of type ApplicationContext resolved to the ApplicationContext instance that the bean is living in.
Note: There are no such default types registered in a plain BeanFactory, not even for the BeanFactory interface itself.
registerResolvableDependency in interface ConfigurableListableBeanFactorydependencyType - the dependency type to register. This will typically
be a base interface such as BeanFactory, with extensions of it resolved
as well if declared as an autowiring dependency (e.g. ListableBeanFactory),
as long as the given value actually implements the extended interface.autowiredValue - the corresponding autowired value. This may also be an
implementation of the ObjectFactory
interface, which allows for lazy resolution of the actual target value.public boolean isAutowireCandidate(java.lang.String beanName,
DependencyDescriptor descriptor)
throws NoSuchBeanDefinitionException
ConfigurableListableBeanFactoryThis method checks ancestor factories as well.
isAutowireCandidate in interface ConfigurableListableBeanFactorybeanName - the name of the bean to checkdescriptor - the descriptor of the dependency to resolveNoSuchBeanDefinitionException - if there is no bean with the given nameprotected boolean isAutowireCandidate(java.lang.String beanName,
DependencyDescriptor descriptor,
AutowireCandidateResolver resolver)
throws NoSuchBeanDefinitionException
beanName - the name of the bean definition to checkdescriptor - the descriptor of the dependency to resolveresolver - the AutowireCandidateResolver to use for the actual resolution algorithmNoSuchBeanDefinitionExceptionprotected boolean isAutowireCandidate(java.lang.String beanName,
RootBeanDefinition mbd,
DependencyDescriptor descriptor,
AutowireCandidateResolver resolver)
beanName - the name of the bean definition to checkmbd - the merged bean definition to checkdescriptor - the descriptor of the dependency to resolveresolver - the AutowireCandidateResolver to use for the actual resolution algorithmpublic BeanDefinition getBeanDefinition(java.lang.String beanName) throws NoSuchBeanDefinitionException
AbstractBeanFactoryDepending on the nature of the concrete bean factory implementation, this operation might be expensive (for example, because of directory lookups in external registries). However, for listable bean factories, this usually just amounts to a local hash lookup: The operation is therefore part of the public interface there. The same implementation can serve for both this template method and the public interface method in that case.
getBeanDefinition in interface ConfigurableListableBeanFactorygetBeanDefinition in interface BeanDefinitionRegistrygetBeanDefinition in class AbstractBeanFactorybeanName - the name of the bean to find a definition fornull)NoSuchBeanDefinitionException - if there is no bean with the given name
defined in this factoryRootBeanDefinition,
ChildBeanDefinition,
ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)public java.util.Iterator<java.lang.String> getBeanNamesIterator()
ConfigurableListableBeanFactoryIncludes bean definition names as well as names of manually registered singleton instances, with bean definition names consistently coming first, analogous to how type/annotation specific retrieval of bean names works.
getBeanNamesIterator in interface ConfigurableListableBeanFactoryListableBeanFactory.containsBeanDefinition(java.lang.String),
SingletonBeanRegistry.registerSingleton(java.lang.String, java.lang.Object),
ListableBeanFactory.getBeanNamesForType(org.springframework.core.ResolvableType),
ListableBeanFactory.getBeanNamesForAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation>)public void clearMetadataCache()
AbstractBeanFactoryTypically triggered after changes to the original bean definitions,
e.g. after applying a BeanFactoryPostProcessor. Note that metadata
for beans which have already been created at this point will be kept around.
clearMetadataCache in interface ConfigurableListableBeanFactoryclearMetadataCache in class AbstractBeanFactoryConfigurableListableBeanFactory.getBeanDefinition(java.lang.String),
ConfigurableBeanFactory.getMergedBeanDefinition(java.lang.String)public void freezeConfiguration()
ConfigurableListableBeanFactoryThis allows the factory to aggressively cache bean definition metadata.
freezeConfiguration in interface ConfigurableListableBeanFactorypublic boolean isConfigurationFrozen()
ConfigurableListableBeanFactoryisConfigurationFrozen in interface ConfigurableListableBeanFactorytrue if the factory's configuration is considered frozenprotected boolean isBeanEligibleForMetadataCaching(java.lang.String beanName)
isBeanEligibleForMetadataCaching in class AbstractBeanFactorybeanName - the name of the beantrue if the bean's metadata may be cached
at this point alreadyfreezeConfiguration()public void preInstantiateSingletons()
throws BeansException
ConfigurableListableBeanFactoryFactoryBeans.
Typically invoked at the end of factory setup, if desired.preInstantiateSingletons in interface ConfigurableListableBeanFactoryBeansException - if one of the singleton beans could not be created.
Note: This may have left the factory with some beans already initialized!
Call ConfigurableBeanFactory.destroySingletons() for full cleanup in this case.ConfigurableBeanFactory.destroySingletons()public void registerBeanDefinition(java.lang.String beanName,
BeanDefinition beanDefinition)
throws BeanDefinitionStoreException
BeanDefinitionRegistryregisterBeanDefinition in interface BeanDefinitionRegistrybeanName - the name of the bean instance to registerbeanDefinition - definition of the bean instance to registerBeanDefinitionStoreException - if the BeanDefinition is invalid
or if there is already a BeanDefinition for the specified bean name
(and we are not allowed to override it)RootBeanDefinition,
ChildBeanDefinitionpublic void removeBeanDefinition(java.lang.String beanName)
throws NoSuchBeanDefinitionException
BeanDefinitionRegistryremoveBeanDefinition in interface BeanDefinitionRegistrybeanName - the name of the bean instance to registerNoSuchBeanDefinitionException - if there is no such bean definitionprotected void resetBeanDefinition(java.lang.String beanName)
beanName - the name of the bean to resetprotected boolean allowAliasOverriding()
allowAliasOverriding in class org.springframework.core.SimpleAliasRegistrypublic void registerSingleton(java.lang.String beanName,
java.lang.Object singletonObject)
throws java.lang.IllegalStateException
SingletonBeanRegistryThe given instance is supposed to be fully initialized; the registry
will not perform any initialization callbacks (in particular, it won't
call InitializingBean's afterPropertiesSet method).
The given instance will not receive any destruction callbacks
(like DisposableBean's destroy method) either.
When running within a full BeanFactory: Register a bean definition instead of an existing instance if your bean is supposed to receive initialization and/or destruction callbacks.
Typically invoked during registry configuration, but can also be used for runtime registration of singletons. As a consequence, a registry implementation should synchronize singleton access; it will have to do this anyway if it supports a BeanFactory's lazy initialization of singletons.
registerSingleton in interface SingletonBeanRegistryregisterSingleton in class DefaultSingletonBeanRegistrybeanName - the name of the beansingletonObject - the existing singleton objectjava.lang.IllegalStateExceptionInitializingBean.afterPropertiesSet(),
DisposableBean.destroy(),
BeanDefinitionRegistry.registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)public void destroySingleton(java.lang.String beanName)
DefaultSingletonBeanRegistrydestroyBean
if a corresponding disposable bean instance is found.destroySingleton in class DefaultSingletonBeanRegistrybeanName - the name of the beanDefaultSingletonBeanRegistry.destroyBean(java.lang.String, org.springframework.beans.factory.DisposableBean)public void destroySingletons()
ConfigurableBeanFactoryAny exception that arises during destruction should be caught and logged instead of propagated to the caller of this method.
destroySingletons in interface ConfigurableBeanFactorydestroySingletons in class DefaultSingletonBeanRegistrypublic <T> NamedBeanHolder<T> resolveNamedBean(java.lang.Class<T> requiredType) throws BeansException
AutowireCapableBeanFactoryThis is effectively a variant of BeanFactory.getBean(Class) which preserves the
bean name of the matching instance.
resolveNamedBean in interface AutowireCapableBeanFactoryrequiredType - type the bean must match; can be an interface or superclass.
null is disallowed.NoSuchBeanDefinitionException - if no matching bean was foundNoUniqueBeanDefinitionException - if more than one matching bean was foundBeansException - if the bean could not be createdBeanFactory.getBean(Class)public java.lang.Object resolveDependency(DependencyDescriptor descriptor, java.lang.String requestingBeanName, java.util.Set<java.lang.String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException
AutowireCapableBeanFactoryresolveDependency in interface AutowireCapableBeanFactorydescriptor - the descriptor for the dependency (field/method/constructor)requestingBeanName - the name of the bean which declares the given dependencyautowiredBeanNames - a Set that all names of autowired beans (used for
resolving the given dependency) are supposed to be added totypeConverter - the TypeConverter to use for populating arrays and collectionsnull if none foundNoSuchBeanDefinitionException - if no matching bean was foundNoUniqueBeanDefinitionException - if more than one matching bean was foundBeansException - if dependency resolution failed for any other reasonDependencyDescriptorpublic java.lang.Object doResolveDependency(DependencyDescriptor descriptor, java.lang.String beanName, java.util.Set<java.lang.String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException
BeansExceptionprotected java.util.Map<java.lang.String,java.lang.Object> findAutowireCandidates(java.lang.String beanName,
java.lang.Class<?> requiredType,
DependencyDescriptor descriptor)
beanName - the name of the bean that is about to be wiredrequiredType - the actual type of bean to look for
(may be an array component type or collection element type)descriptor - the descriptor of the dependency to resolvenull)BeansException - in case of errorsAbstractAutowireCapableBeanFactory.autowireByType(java.lang.String, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.BeanWrapper, org.springframework.beans.MutablePropertyValues),
AbstractAutowireCapableBeanFactory.autowireConstructor(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.reflect.Constructor<?>[], java.lang.Object[])protected java.lang.String determineAutowireCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
DependencyDescriptor descriptor)
Looks for @Primary and @Priority (in that order).
candidates - a Map of candidate names and candidate instances
that match the required type, as returned by findAutowireCandidates(java.lang.String, java.lang.Class<?>, org.springframework.beans.factory.config.DependencyDescriptor)descriptor - the target dependency to match againstnull if none foundprotected java.lang.String determinePrimaryCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
java.lang.Class<?> requiredType)
candidates - a Map of candidate names and candidate instances
(or candidate classes if not created yet) that match the required typerequiredType - the target dependency type to match againstnull if none foundisPrimary(String, Object)protected java.lang.String determineHighestPriorityCandidate(java.util.Map<java.lang.String,java.lang.Object> candidates,
java.lang.Class<?> requiredType)
Based on @javax.annotation.Priority. As defined by the related
Ordered interface, the lowest value has
the highest priority.
candidates - a Map of candidate names and candidate instances
(or candidate classes if not created yet) that match the required typerequiredType - the target dependency type to match againstnull if none foundgetPriority(Object)protected boolean isPrimary(java.lang.String beanName,
java.lang.Object beanInstance)
beanName - the name of the beanbeanInstance - the corresponding bean instance (can be null)protected java.lang.Integer getPriority(java.lang.Object beanInstance)
javax.annotation.Priority annotation.
The default implementation delegates to the specified
dependency comparator, checking its
method if it is an extension of
Spring's common OrderComparator - typically, an
AnnotationAwareOrderComparator.
If no such comparator is present, this implementation returns null.
beanInstance - the bean instance to check (can be null)null if none is setprotected boolean matchesBeanName(java.lang.String beanName,
java.lang.String candidateName)
public java.lang.String toString()
toString in class java.lang.Objectprotected java.lang.Object writeReplace()
throws java.io.ObjectStreamException
java.io.ObjectStreamException