public interface ListableBeanFactory extends BeanFactory
BeanFactory
interface to be implemented by bean factories
that can enumerate all their bean instances, rather than attempting bean lookup
by name one by one as requested by clients. BeanFactory implementations that
preload all their bean definitions (such as XML-based factories) may implement
this interface.
If this is a HierarchicalBeanFactory
, the return values will not
take any BeanFactory hierarchy into account, but will relate only to the beans
defined in the current factory. Use the BeanFactoryUtils
helper class
to consider beans in ancestor factories too.
The methods in this interface will just respect bean definitions of this factory.
They will ignore any singleton beans that have been registered by other means like
ConfigurableBeanFactory
's
registerSingleton
method, with the exception of
getBeanNamesOfType
and getBeansOfType
which will check
such manually registered singletons too. Of course, BeanFactory's getBean
does allow transparent access to such special beans as well. However, in typical
scenarios, all beans will be defined by external bean definitions anyway, so most
applications don't need to worry about this differentiation.
NOTE: With the exception of getBeanDefinitionCount
and containsBeanDefinition
, the methods in this interface
are not designed for frequent invocation. Implementations may be slow.
HierarchicalBeanFactory
,
BeanFactoryUtils
FACTORY_BEAN_PREFIX
Modifier and Type | Method and Description |
---|---|
boolean |
containsBeanDefinition(java.lang.String beanName)
Check if this bean factory contains a bean definition with the given name.
|
<A extends java.lang.annotation.Annotation> |
findAnnotationOnBean(java.lang.String beanName,
java.lang.Class<A> annotationType)
Find an
Annotation of annotationType on the specified
bean, traversing its interfaces and super classes if no annotation can be
found on the given class itself. |
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(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. |
<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. |
containsBean, getAliases, getBean, getBean, getBean, getBean, getBean, getType, isPrototype, isSingleton, isTypeMatch, isTypeMatch
boolean containsBeanDefinition(java.lang.String beanName)
Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.
beanName
- the name of the bean to look forBeanFactory.containsBean(java.lang.String)
int getBeanDefinitionCount()
Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.
java.lang.String[] getBeanDefinitionNames()
Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.
java.lang.String[] getBeanNamesForType(ResolvableType type)
getObjectType
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.
type
- the class or interface to match, or null
for all bean namesFactoryBean.getObjectType()
,
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)
java.lang.String[] getBeanNamesForType(java.lang.Class<?> type)
getObjectType
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.
type
- the class or interface to match, or null
for all bean namesFactoryBean.getObjectType()
,
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)
java.lang.String[] getBeanNamesForType(java.lang.Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)
getObjectType
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.
type
- 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)
<T> java.util.Map<java.lang.String,T> getBeansOfType(java.lang.Class<T> type) throws BeansException
getObjectType
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.
type
- the class or interface to match, or null
for all concrete beansBeansException
- if a bean could not be createdFactoryBean.getObjectType()
,
BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
<T> java.util.Map<java.lang.String,T> getBeansOfType(java.lang.Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException
getObjectType
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.
type
- 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)
java.lang.String[] getBeanNamesForAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Class
has the supplied Annotation
type, without creating any bean instances yet.annotationType
- the type of annotation to look forjava.util.Map<java.lang.String,java.lang.Object> getBeansWithAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) throws BeansException
Class
has the supplied Annotation
type,
returning a Map of bean names with corresponding bean instances.annotationType
- the type of annotation to look forBeansException
- if a bean could not be created<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.beanName
- the name of the bean to look for annotations onannotationType
- the annotation class to look fornull
NoSuchBeanDefinitionException
- if there is no bean with the given name