public abstract class BeanFactoryUtils
extends java.lang.Object
ListableBeanFactory
interface.
Returns bean counts, bean names or bean instances, taking into account the nesting hierarchy of a bean factory (which the methods defined on the ListableBeanFactory interface don't, in contrast to the methods defined on the BeanFactory interface).
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
GENERATED_BEAN_NAME_SEPARATOR
Separator for generated bean names.
|
Constructor and Description |
---|
BeanFactoryUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String[] |
beanNamesForAnnotationIncludingAncestors(ListableBeanFactory lbf,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Get all bean names whose
Class has the supplied Annotation
type, including those defined in ancestor factories, without creating any bean
instances yet. |
static java.lang.String[] |
beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
java.lang.Class<?> type)
Get all bean names for the given type, including those defined in ancestor
factories.
|
static java.lang.String[] |
beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
java.lang.Class<?> type,
boolean includeNonSingletons,
boolean allowEagerInit)
Get all bean names for the given type, including those defined in ancestor
factories.
|
static java.lang.String[] |
beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
ResolvableType type)
Get all bean names for the given type, including those defined in ancestor
factories.
|
static java.lang.String[] |
beanNamesIncludingAncestors(ListableBeanFactory lbf)
Return all bean names in the factory, including ancestor factories.
|
static <T> T |
beanOfType(ListableBeanFactory lbf,
java.lang.Class<T> type)
Return a single bean of the given type or subtypes, not looking in ancestor
factories.
|
static <T> T |
beanOfType(ListableBeanFactory lbf,
java.lang.Class<T> type,
boolean includeNonSingletons,
boolean allowEagerInit)
Return a single bean of the given type or subtypes, not looking in ancestor
factories.
|
static <T> T |
beanOfTypeIncludingAncestors(ListableBeanFactory lbf,
java.lang.Class<T> type)
Return a single bean of the given type or subtypes, also picking up beans
defined in ancestor bean factories if the current bean factory is a
HierarchicalBeanFactory.
|
static <T> T |
beanOfTypeIncludingAncestors(ListableBeanFactory lbf,
java.lang.Class<T> type,
boolean includeNonSingletons,
boolean allowEagerInit)
Return a single bean of the given type or subtypes, also picking up beans
defined in ancestor bean factories if the current bean factory is a
HierarchicalBeanFactory.
|
static <T> java.util.Map<java.lang.String,T> |
beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
java.lang.Class<T> type)
Return all beans of the given type or subtypes, also picking up beans defined in
ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
|
static <T> java.util.Map<java.lang.String,T> |
beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
java.lang.Class<T> type,
boolean includeNonSingletons,
boolean allowEagerInit)
Return all beans of the given type or subtypes, also picking up beans defined in
ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
|
static int |
countBeansIncludingAncestors(ListableBeanFactory lbf)
Count all beans in any hierarchy in which this factory participates.
|
static boolean |
isFactoryDereference(java.lang.String name)
Return whether the given name is a factory dereference
(beginning with the factory dereference prefix).
|
static boolean |
isGeneratedBeanName(java.lang.String name)
Return whether the given name is a bean name which has been generated
by the default naming strategy (containing a "#..." part).
|
static java.lang.String |
originalBeanName(java.lang.String name)
Extract the "raw" bean name from the given (potentially generated) bean name,
excluding any "#..." suffixes which might have been added for uniqueness.
|
static java.lang.String |
transformedBeanName(java.lang.String name)
Return the actual bean name, stripping out the factory dereference
prefix (if any, also stripping repeated factory prefixes if found).
|
public static final java.lang.String GENERATED_BEAN_NAME_SEPARATOR
public static boolean isFactoryDereference(@Nullable java.lang.String name)
name
- the name of the beanBeanFactory.FACTORY_BEAN_PREFIX
public static java.lang.String transformedBeanName(java.lang.String name)
name
- the name of the beanBeanFactory.FACTORY_BEAN_PREFIX
public static boolean isGeneratedBeanName(@Nullable java.lang.String name)
name
- the name of the beanGENERATED_BEAN_NAME_SEPARATOR
,
BeanDefinitionReaderUtils.generateBeanName(org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry)
,
DefaultBeanNameGenerator
public static java.lang.String originalBeanName(java.lang.String name)
name
- the potentially generated bean nameGENERATED_BEAN_NAME_SEPARATOR
public static int countBeansIncludingAncestors(ListableBeanFactory lbf)
Beans that are "overridden" (specified in a descendant factory with the same name) are only counted once.
lbf
- the bean factorypublic static java.lang.String[] beanNamesIncludingAncestors(ListableBeanFactory lbf)
lbf
- the bean factorybeanNamesForTypeIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory, org.springframework.core.ResolvableType)
public static java.lang.String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, ResolvableType type)
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.
This version of beanNamesForTypeIncludingAncestors
automatically
includes prototypes and FactoryBeans.
lbf
- the bean factorytype
- the type that beans must match (as a ResolvableType
)public static java.lang.String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class<?> type)
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.
This version of beanNamesForTypeIncludingAncestors
automatically
includes prototypes and FactoryBeans.
lbf
- the bean factorytype
- the type that beans must match (as a Class
)public static java.lang.String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)
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).
lbf
- the bean factoryincludeNonSingletons
- 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.type
- the type that beans must matchpublic static <T> java.util.Map<java.lang.String,T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class<T> type) throws BeansException
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.
Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.
lbf
- the bean factorytype
- type of bean to matchBeansException
- if a bean could not be createdpublic static <T> java.util.Map<java.lang.String,T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException
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).
Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.
lbf
- the bean factorytype
- type of bean to matchincludeNonSingletons
- 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 createdpublic static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class<T> type) throws BeansException
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.
This version of beanOfTypeIncludingAncestors
automatically includes
prototypes and FactoryBeans.
Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.
lbf
- the bean factorytype
- type of bean to matchNoSuchBeanDefinitionException
- 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 createdpublic static java.lang.String[] beanNamesForAnnotationIncludingAncestors(ListableBeanFactory lbf, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Class
has the supplied Annotation
type, including those defined in ancestor factories, without creating any bean
instances yet. Will return unique names in case of overridden bean definitions.lbf
- the bean factoryannotationType
- the type of annotation to look forpublic static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf, java.lang.Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException
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).
Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.
lbf
- the bean factorytype
- type of bean to matchincludeNonSingletons
- 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.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 createdpublic static <T> T beanOfType(ListableBeanFactory lbf, java.lang.Class<T> type) throws BeansException
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.
This version of beanOfType
automatically includes
prototypes and FactoryBeans.
lbf
- the bean factorytype
- type of bean to matchNoSuchBeanDefinitionException
- 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 createdpublic static <T> T beanOfType(ListableBeanFactory lbf, java.lang.Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException
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).
lbf
- the bean factorytype
- type of bean to matchincludeNonSingletons
- 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.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 created