Package org.springframework.beans.factory

# Interface ListableBeanFactory

All Superinterfaces:
  `BeanFactory`

All Known Subinterfaces:
  `ConfigurableListableBeanFactory`

All Known Implementing Classes:
  `DefaultListableBeanFactory`, `StaticListableBeanFactory`

public interface ListableBeanFactory extends BeanFactory
Extension of the `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 `getBeanNamesForType` 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.

Since:
  16 April 2001
Author:
  Rod Johnson, Juergen Hoeller
See Also:
  - `HierarchicalBeanFactory`
  - `BeanFactoryUtils`

## Field Summary

### Fields inherited from interface org.springframework.beans.factory.BeanFactory

`FACTORY_BEAN_PREFIX, FACTORY_BEAN_PREFIX_CHAR`

## Method Summary

Modifier and Type
Method
Description
`boolean`
`containsBeanDefinition(String beanName)`
Check if this bean factory contains a bean definition with the given name.
`<A extends Annotation>
Set<A>`
`findAllAnnotationsOnBean(String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)`
Find all `Annotation` instances of `annotationType` on the specified bean, traversing its interfaces and superclasses if no annotation can be found on the given class itself, as well as checking the bean's factory method (if any).
`<A extends Annotation>
A`
`findAnnotationOnBean(String beanName, Class<A> annotationType)`
Find an `Annotation` of `annotationType` on the specified bean, traversing its interfaces and superclasses if no annotation can be found on the given class itself, as well as checking the bean's factory method (if any).
`<A extends Annotation>
A`
`findAnnotationOnBean(String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)`
Find an `Annotation` of `annotationType` on the specified bean, traversing its interfaces and superclasses if no annotation can be found on the given class itself, as well as checking the bean's factory method (if any).
`int`
`getBeanDefinitionCount()`
Return the number of beans defined in the factory.
`String[]`
`getBeanDefinitionNames()`
Return the names of all beans defined in this factory.
`String[]`
`getBeanNamesForAnnotation(Class<? extends Annotation> annotationType)`
Find all names of beans which are annotated with the supplied `Annotation` type, without creating corresponding bean instances yet.
`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 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.
`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.
`String[]`
`getBeanNamesForType(org.springframework.core.ResolvableType 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.
`<T> ObjectProvider<T>`
`getBeanProvider(Class<T> requiredType, boolean allowEagerInit)`
Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
`<T> ObjectProvider<T>`
`getBeanProvider(org.springframework.core.ResolvableType requiredType, boolean allowEagerInit)`
Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
`<T> Map<String,T>`
`getBeansOfType(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> Map<String,T>`
`getBeansOfType(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.
`Map<String,Object>`
`getBeansWithAnnotation(Class<? extends Annotation> annotationType)`
Find all beans which are annotated with the supplied `Annotation` type, returning a Map of bean names with corresponding bean instances.

### Methods inherited from interface org.springframework.beans.factory.BeanFactory

`containsBean, getAliases, getBean, getBean, getBean, getBean, getBean, getBeanProvider, getBeanProvider, getType, getType, isPrototype, isSingleton, isTypeMatch, isTypeMatch`

## Method Details

### containsBeanDefinition

boolean containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given name.

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.

Parameters:
  `beanName` - the name of the bean to look for
Returns:
  if this bean factory contains a bean definition with the given name
See Also:
  - `BeanFactory.containsBean(java.lang.String)`

### getBeanDefinitionCount

int getBeanDefinitionCount()
Return the number of beans defined in the factory.

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.

Returns:
  the number of beans defined in the factory

### getBeanDefinitionNames

String[] getBeanDefinitionNames()
Return the names of all beans defined in this factory.

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.

Returns:
  the names of all beans defined in this factory, or an empty array if none defined

### getBeanProvider

<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType, boolean allowEagerInit)
Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.

Parameters:
  `requiredType` - type the bean must match; can be an interface or superclass
  `allowEagerInit` - whether stream access may introspect 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. Only actually necessary initialization for type checking purposes will be performed; constructor and method invocations will still be avoided as far as possible.
Returns:
  a corresponding provider handle
Since:
  5.3
See Also:
  - `getBeanProvider(ResolvableType, boolean)`
  - `BeanFactory.getBeanProvider(Class)`
  - `getBeansOfType(Class, boolean, boolean)`
  - `getBeanNamesForType(Class, boolean, boolean)`

### getBeanProvider

<T> ObjectProvider<T> getBeanProvider(org.springframework.core.ResolvableType requiredType, boolean allowEagerInit)
Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.

Parameters:
  `requiredType` - type the bean must match; can be a generic type declaration. Note that collection types are not supported here, in contrast to reflective injection points. For programmatically retrieving a list of beans matching a specific type, specify the actual bean type as an argument here and subsequently use `ObjectProvider.orderedStream()` or its lazy streaming/iteration options.
  `allowEagerInit` - whether stream access may introspect 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. Only actually necessary initialization for type checking purposes will be performed; constructor and method invocations will still be avoided as far as possible.
Returns:
  a corresponding provider handle
Since:
  5.3
See Also:
  - `BeanFactory.getBeanProvider(ResolvableType)`
  - `ObjectProvider.iterator()`
  - `ObjectProvider.stream()`
  - `ObjectProvider.orderedStream()`
  - `getBeanNamesForType(ResolvableType, boolean, boolean)`

### getBeanNamesForType

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.

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.

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.

Parameters:
  `type` - the generically typed class or interface to match
Returns:
  the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
Since:
  4.2
See Also:
  - `BeanFactory.isTypeMatch(String, ResolvableType)`
  - `FactoryBean.getObjectType()`
  - `BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)`

### getBeanNamesForType

String[] getBeanNamesForType(org.springframework.core.ResolvableType 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.

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.

Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.

Parameters:
  `type` - the generically typed class or interface to match
  `includeNonSingletons` - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
  `allowEagerInit` - whether to introspect 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. Only actually necessary initialization for type checking purposes will be performed; constructor and method invocations will still be avoided as far as possible.
Returns:
  the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
Since:
  5.2
See Also:
  - `FactoryBean.getObjectType()`
  - `BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType, boolean, boolean)`

### getBeanNamesForType

String[] getBeanNamesForType(@Nullable 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.

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.

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.

Parameters:
  `type` - the class or interface to match, or `null` for all bean names
Returns:
  the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
See Also:
  - `FactoryBean.getObjectType()`
  - `BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)`

### getBeanNamesForType

String[] getBeanNamesForType(@Nullable 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.

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.

Bean names returned by this method should always return bean names in the order of definition in the backend configuration, as far as possible.

Parameters:
  `type` - the class or interface to match, or `null` for all bean names
  `includeNonSingletons` - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
  `allowEagerInit` - whether to introspect 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. Only actually necessary initialization for type checking purposes will be performed; constructor and method invocations will still be avoided as far as possible.
Returns:
  the names of beans (or objects created by FactoryBeans) matching the given object type (including subclasses), or an empty array if none
See Also:
  - `FactoryBean.getObjectType()`
  - `BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)`

### getBeansOfType

<T> Map<String,T> getBeansOfType(@Nullable Class<T> type) throws BeansException
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.

NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well. Also, it suppresses exceptions for beans that are currently in creation in a circular reference scenario: typically, references back to the caller of this method.

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.

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.

Consider `getBeanNamesForType(Class)` with selective `BeanFactory.getBean(java.lang.String)` calls for specific bean names in preference to this Map-based retrieval method. Aside from lazy instantiation benefits, this also avoids any exception suppression.

Parameters:
  `type` - the class or interface to match, or `null` for all concrete beans
Returns:
  a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values
Throws:
  `BeansException` - if a bean could not be created
Since:
  1.1.2
See Also:
  - `FactoryBean.getObjectType()`
  - `BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class)`

### getBeansOfType

<T> Map<String,T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException
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.

NOTE: This method introspects top-level beans only. It does not check nested beans which might match the specified type as well. Also, it suppresses exceptions for beans that are currently in creation in a circular reference scenario: typically, references back to the caller of this method.

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.

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.

Consider `getBeanNamesForType(Class)` with selective `BeanFactory.getBean(java.lang.String)` calls for specific bean names in preference to this Map-based retrieval method. Aside from lazy instantiation benefits, this also avoids any exception suppression.

Parameters:
  `type` - the class or interface to match, or `null` for all concrete beans
  `includeNonSingletons` - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
  `allowEagerInit` - whether to introspect 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. Only actually necessary initialization for type checking purposes will be performed; constructor and method invocations will still be avoided as far as possible.
Returns:
  a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values
Throws:
  `BeansException` - if a bean could not be created
See Also:
  - `FactoryBean.getObjectType()`
  - `BeanFactoryUtils.beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)`

### getBeanNamesForAnnotation

String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType)
Find all names of beans which are annotated with the supplied `Annotation` type, without creating corresponding bean instances yet.

Note that this method considers objects created by FactoryBeans, which means that FactoryBeans will get initialized in order to determine their object type.

Parameters:
  `annotationType` - the type of annotation to look for (at class, interface or factory method level of the specified bean)
Returns:
  the names of all matching beans
Since:
  4.0
See Also:
  - `getBeansWithAnnotation(Class)`
  - `findAnnotationOnBean(String, Class)`

### getBeansWithAnnotation

Map<String,Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException
Find all beans which are annotated with the supplied `Annotation` type, returning a Map of bean names with corresponding bean instances.

Note that this method considers objects created by FactoryBeans, which means that FactoryBeans will get initialized in order to determine their object type.

Parameters:
  `annotationType` - the type of annotation to look for (at class, interface or factory method level of the specified bean)
Returns:
  a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values
Throws:
  `BeansException` - if a bean could not be created
Since:
  3.0
See Also:
  - `findAnnotationOnBean(String, Class)`
  - `findAnnotationOnBean(String, Class, boolean)`
  - `findAllAnnotationsOnBean(String, Class, boolean)`

### findAnnotationOnBean

@Nullable <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) throws NoSuchBeanDefinitionException
Find an `Annotation` of `annotationType` on the specified bean, traversing its interfaces and superclasses if no annotation can be found on the given class itself, as well as checking the bean's factory method (if any).

Parameters:
  `beanName` - the name of the bean to look for annotations on
  `annotationType` - the type of annotation to look for (at class, interface or factory method level of the specified bean)
Returns:
  the annotation of the given type if found, or `null` otherwise
Throws:
  `NoSuchBeanDefinitionException` - if there is no bean with the given name
Since:
  3.0
See Also:
  - `findAnnotationOnBean(String, Class, boolean)`
  - `findAllAnnotationsOnBean(String, Class, boolean)`
  - `getBeanNamesForAnnotation(Class)`
  - `getBeansWithAnnotation(Class)`
  - `BeanFactory.getType(String)`

### findAnnotationOnBean

@Nullable <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException
Find an `Annotation` of `annotationType` on the specified bean, traversing its interfaces and superclasses if no annotation can be found on the given class itself, as well as checking the bean's factory method (if any).

Parameters:
  `beanName` - the name of the bean to look for annotations on
  `annotationType` - the type of annotation to look for (at class, interface or factory method level of the specified bean)
  `allowFactoryBeanInit` - whether a `FactoryBean` may get initialized just for the purpose of determining its object type
Returns:
  the annotation of the given type if found, or `null` otherwise
Throws:
  `NoSuchBeanDefinitionException` - if there is no bean with the given name
Since:
  5.3.14
See Also:
  - `findAnnotationOnBean(String, Class)`
  - `findAllAnnotationsOnBean(String, Class, boolean)`
  - `getBeanNamesForAnnotation(Class)`
  - `getBeansWithAnnotation(Class)`
  - `BeanFactory.getType(String, boolean)`

### findAllAnnotationsOnBean

<A extends Annotation> Set<A> findAllAnnotationsOnBean(String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException
Find all `Annotation` instances of `annotationType` on the specified bean, traversing its interfaces and superclasses if no annotation can be found on the given class itself, as well as checking the bean's factory method (if any).

Parameters:
  `beanName` - the name of the bean to look for annotations on
  `annotationType` - the type of annotation to look for (at class, interface or factory method level of the specified bean)
  `allowFactoryBeanInit` - whether a `FactoryBean` may get initialized just for the purpose of determining its object type
Returns:
  the set of annotations of the given type found (potentially empty)
Throws:
  `NoSuchBeanDefinitionException` - if there is no bean with the given name
Since:
  6.0
See Also:
  - `getBeanNamesForAnnotation(Class)`
  - `findAnnotationOnBean(String, Class, boolean)`
  - `BeanFactory.getType(String, boolean)`