|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.beans.factory.support.AbstractBeanFactory org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory org.springframework.beans.factory.support.DefaultListableBeanFactory
public class DefaultListableBeanFactory
Default implementation of the ListableBeanFactory and BeanDefinitionRegistry interfaces: a full-fledged bean factory based on bean definitions.
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.
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.
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.
ListableBeanFactory
,
StaticListableBeanFactory
,
PropertiesBeanDefinitionReader
,
XmlBeanDefinitionReader
Field Summary |
---|
Fields inherited from class org.springframework.beans.factory.support.AbstractBeanFactory |
---|
logger |
Fields inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory |
---|
AUTOWIRE_AUTODETECT, AUTOWIRE_BY_NAME, AUTOWIRE_BY_TYPE, AUTOWIRE_CONSTRUCTOR |
Constructor Summary | |
---|---|
DefaultListableBeanFactory()
Create a new DefaultListableBeanFactory. |
|
DefaultListableBeanFactory(BeanFactory parentBeanFactory)
Create a new DefaultListableBeanFactory with the given parent. |
Method Summary | |
---|---|
boolean |
containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given name. |
protected Map |
findMatchingBeans(Class requiredType)
Find bean instances that match the required type. |
BeanDefinition |
getBeanDefinition(String beanName)
Return the bean definition for the given bean name. |
int |
getBeanDefinitionCount()
Return the number of beans defined in the factory. |
String[] |
getBeanDefinitionNames()
Return the names of all beans defined in this factory. |
String[] |
getBeanDefinitionNames(Class type)
Return the names of beans matching the given type (including subclasses), judging from the bean definitions. |
String[] |
getBeanNamesForType(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. |
String[] |
getBeanNamesForType(Class type,
boolean includePrototypes,
boolean includeFactoryBeans)
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. |
Map |
getBeansOfType(Class 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. |
Map |
getBeansOfType(Class type,
boolean includePrototypes,
boolean includeFactoryBeans)
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. |
void |
preInstantiateSingletons()
Ensure that all non-lazy-init singletons are instantiated, also considering FactoryBeans. |
void |
registerBeanDefinition(String beanName,
BeanDefinition beanDefinition)
Register a new bean definition with this registry. |
void |
setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
Set if it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former. |
String |
toString()
|
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.config.ConfigurableListableBeanFactory |
---|
ignoreDependencyInterface, ignoreDependencyType |
Methods inherited from interface org.springframework.beans.factory.config.AutowireCapableBeanFactory |
---|
applyBeanPostProcessorsAfterInitialization, applyBeanPostProcessorsBeforeInitialization, applyBeanPropertyValues, autowire, autowireBeanProperties |
Methods inherited from interface org.springframework.beans.factory.config.ConfigurableBeanFactory |
---|
addBeanPostProcessor, containsSingleton, destroySingletons, getBeanPostProcessorCount, registerAlias, registerCustomEditor, registerSingleton, setParentBeanFactory |
Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory |
---|
getParentBeanFactory |
Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry |
---|
getAliases, registerAlias |
Constructor Detail |
---|
public DefaultListableBeanFactory()
public DefaultListableBeanFactory(BeanFactory parentBeanFactory)
Method Detail |
---|
public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
public boolean containsBeanDefinition(String beanName)
AbstractBeanFactory
containsBean
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 ListableBeanFactory
containsBeanDefinition
in interface BeanDefinitionRegistry
containsBeanDefinition
in class AbstractBeanFactory
beanName
- the name of the bean to look for
AbstractBeanFactory.containsBean(java.lang.String)
,
ListableBeanFactory.containsBeanDefinition(java.lang.String)
public int getBeanDefinitionCount()
ListableBeanFactory
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' countBeansIncludingAncestors
to include beans in ancestor factories too.
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionCount
in interface ListableBeanFactory
getBeanDefinitionCount
in interface BeanDefinitionRegistry
BeanFactoryUtils.countBeansIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory)
public String[] getBeanDefinitionNames()
ListableBeanFactory
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesIncludingAncestors
to include beans in ancestor factories too.
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionNames
in interface ListableBeanFactory
getBeanDefinitionNames
in interface BeanDefinitionRegistry
BeanFactoryUtils.beanNamesIncludingAncestors(ListableBeanFactory)
public String[] getBeanDefinitionNames(Class type)
ListableBeanFactory
Does not consider objects created by FactoryBeans but rather the
FactoryBean classes themselves, avoiding instantiation of any beans. Use
getBeanNamesForType
to match objects created by FactoryBeans.
Does not consider any hierarchy this factory may participate in.
Use BeanFactoryUtils' beanNamesIncludingAncestors
to include beans in ancestor factories too.
Note: Ignores any singleton beans that have been registered by other means than bean definitions.
getBeanDefinitionNames
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all bean names
ListableBeanFactory.getBeanNamesForType(java.lang.Class)
,
BeanFactoryUtils.beanNamesIncludingAncestors(ListableBeanFactory, Class)
public String[] getBeanNamesForType(Class type)
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
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 getBeanNamesOfType(type, true, true)
.
getBeanNamesForType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all bean names
FactoryBean.getObjectType()
,
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)
public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean includeFactoryBeans)
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
Does consider objects created by FactoryBeans if the "includeFactoryBeans" 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 "includeFactoryBeans" 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.
getBeanNamesForType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all bean namesincludePrototypes
- whether to include prototype beans too
or just singletons (also applies to FactoryBeans)includeFactoryBeans
- whether to include objects created by
FactoryBeans (or by factory methods with a "factory-bean" reference)
too, or just conventional beans. Note that FactoryBeans need to be
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 Map getBeansOfType(Class type) throws BeansException
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
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)
.
getBeansOfType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all concrete beans
BeansException
- if a bean could not be createdFactoryBean.getObjectType()
,
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
public Map getBeansOfType(Class type, boolean includePrototypes, boolean includeFactoryBeans) throws BeansException
ListableBeanFactory
getObjectType
in the case of FactoryBeans.
Does consider objects created by FactoryBeans if the "includeFactoryBeans" 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 "includeFactoryBeans" 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.
getBeansOfType
in interface ListableBeanFactory
type
- the class or interface to match, or null
for all concrete beansincludePrototypes
- whether to include prototype beans too
or just singletons (also applies to FactoryBeans)includeFactoryBeans
- whether to include objects created by
FactoryBeans (or by factory methods with a "factory-bean" reference)
too, or just conventional beans. Note that FactoryBeans need to be
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 void preInstantiateSingletons() throws BeansException
ConfigurableListableBeanFactory
As this is a startup method, it should destroy already created singletons if it fails, to avoid dangling resources. In other words, after invocation of that method, either all or no singletons at all should be instantiated.
preInstantiateSingletons
in interface ConfigurableListableBeanFactory
BeansException
- if one of the singleton beans could not be createdpublic void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException
BeanDefinitionRegistry
registerBeanDefinition
in interface BeanDefinitionRegistry
beanName
- the name of the bean instance to registerbeanDefinition
- definition of the bean instance to register
BeanDefinitionStoreException
RootBeanDefinition
,
ChildBeanDefinition
public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException
AbstractBeanFactory
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.
getBeanDefinition
in interface ConfigurableListableBeanFactory
getBeanDefinition
in interface BeanDefinitionRegistry
getBeanDefinition
in class AbstractBeanFactory
beanName
- name of the bean to find a definition for
NoSuchBeanDefinitionException
- if there is no bean with the given nameRootBeanDefinition
,
ChildBeanDefinition
,
ConfigurableListableBeanFactory.getBeanDefinition(java.lang.String)
protected Map findMatchingBeans(Class requiredType)
AbstractAutowireCapableBeanFactory
findMatchingBeans
in class AbstractAutowireCapableBeanFactory
requiredType
- the type of the beans to look up
null
if none foundAbstractAutowireCapableBeanFactory.autowireByType(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, org.springframework.beans.BeanWrapper, org.springframework.beans.MutablePropertyValues)
,
AbstractAutowireCapableBeanFactory.autowireConstructor(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition)
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |