public abstract class BeanFactoryUtils extends 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 String |
GENERATED_BEAN_NAME_SEPARATOR
Separator for generated bean names.
|
Constructor and Description |
---|
BeanFactoryUtils() |
Modifier and Type | Method and Description |
---|---|
static String[] |
beanNamesForAnnotationIncludingAncestors(ListableBeanFactory lbf,
Class<? extends 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 String[] |
beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
Class<?> type)
Get all bean names for the given type, including those defined in ancestor
factories.
|
static String[] |
beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
Class<?> type,
boolean includeNonSingletons,
boolean allowEagerInit)
Get all bean names for the given type, including those defined in ancestor
factories.
|
static String[] |
beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
ResolvableType type)
Get all bean names for the given type, including those defined in ancestor
factories.
|
static String[] |
beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
ResolvableType type,
boolean includeNonSingletons,
boolean allowEagerInit)
Get all bean names for the given type, including those defined in ancestor
factories.
|
static String[] |
beanNamesIncludingAncestors(ListableBeanFactory lbf)
Return all bean names in the factory, including ancestor factories.
|
static <T> T |
beanOfType(ListableBeanFactory lbf,
Class<T> type)
Return a single bean of the given type or subtypes, not looking in ancestor
factories.
|
static <T> T |
beanOfType(ListableBeanFactory lbf,
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,
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,
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> Map<String,T> |
beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
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> Map<String,T> |
beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
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(String name)
Return whether the given name is a factory dereference
(beginning with the factory dereference prefix).
|
static boolean |
isGeneratedBeanName(String name)
Return whether the given name is a bean name which has been generated
by the default naming strategy (containing a "#..." part).
|
static String |
originalBeanName(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 String |
transformedBeanName(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 String GENERATED_BEAN_NAME_SEPARATOR
public static boolean isFactoryDereference(@Nullable String name)
name
- the name of the beanBeanFactory.FACTORY_BEAN_PREFIX
public static String transformedBeanName(String name)
name
- the name of the beanBeanFactory.FACTORY_BEAN_PREFIX
public static boolean isGeneratedBeanName(@Nullable 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 String originalBeanName(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 factorybeanNamesIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory)
public static String[] beanNamesIncludingAncestors(ListableBeanFactory lbf)
lbf
- the bean factorybeanNamesForTypeIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory, org.springframework.core.ResolvableType)
public static 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
)ListableBeanFactory.getBeanNamesForType(ResolvableType)
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, ResolvableType 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 match (as a ResolvableType
)ListableBeanFactory.getBeanNamesForType(ResolvableType, boolean, boolean)
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, 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
)ListableBeanFactory.getBeanNamesForType(Class)
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, 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 matchListableBeanFactory.getBeanNamesForType(Class, boolean, boolean)
public static String[] beanNamesForAnnotationIncludingAncestors(ListableBeanFactory lbf, Class<? extends 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 forListableBeanFactory.getBeanNamesForAnnotation(Class)
public static <T> Map<String,T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf, 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 createdListableBeanFactory.getBeansOfType(Class)
public static <T> Map<String,T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf, 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 createdListableBeanFactory.getBeansOfType(Class, boolean, boolean)
public static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf, 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 createdbeansOfTypeIncludingAncestors(ListableBeanFactory, Class)
public static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf, 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 createdbeansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
public static <T> T beanOfType(ListableBeanFactory lbf, 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 createdListableBeanFactory.getBeansOfType(Class)
public static <T> T beanOfType(ListableBeanFactory lbf, 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 createdListableBeanFactory.getBeansOfType(Class, boolean, boolean)