Method from org.springframework.beans.factory.BeanFactoryUtils Detail: |
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
Class type) {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
String[] result = lbf.getBeanNamesForType(type);
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
String[] parentResult = beanNamesForTypeIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), type);
List resultList = new ArrayList();
resultList.addAll(Arrays.asList(result));
for (int i = 0; i < parentResult.length; i++) {
String beanName = parentResult[i];
if (!resultList.contains(beanName) && !hbf.containsLocalBean(beanName)) {
resultList.add(beanName);
}
}
result = StringUtils.toStringArray(resultList);
}
}
return result;
}
Get all bean names for the given type, including those defined in ancestor
factories. Will return unique names in case of overridden bean definitions.
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. |
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
Class type,
boolean includeNonSingletons,
boolean allowEagerInit) {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
String[] parentResult = beanNamesForTypeIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit);
List resultList = new ArrayList();
resultList.addAll(Arrays.asList(result));
for (int i = 0; i < parentResult.length; i++) {
String beanName = parentResult[i];
if (!resultList.contains(beanName) && !hbf.containsLocalBean(beanName)) {
resultList.add(beanName);
}
}
result = StringUtils.toStringArray(resultList);
}
}
return result;
}
Get all bean names for the given type, including those defined in ancestor
factories. Will return unique names in case of overridden bean definitions.
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). |
public static String[] beanNamesIncludingAncestors(ListableBeanFactory lbf) {
return beanNamesForTypeIncludingAncestors(lbf, Object.class);
}
Return all bean names in the factory, including ancestor factories. |
public static Object beanOfType(ListableBeanFactory lbf,
Class type) throws BeansException {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
Map beansOfType = lbf.getBeansOfType(type);
if (beansOfType.size() == 1) {
return beansOfType.values().iterator().next();
}
else {
throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size());
}
}
Return a single bean of the given type or subtypes, not looking in ancestor
factories. Useful convenience method when we expect a single bean and
don't care about the bean name.
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. |
public static Object beanOfType(ListableBeanFactory lbf,
Class type,
boolean includeNonSingletons,
boolean allowEagerInit) throws BeansException {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
Map beansOfType = lbf.getBeansOfType(type, includeNonSingletons, allowEagerInit);
if (beansOfType.size() == 1) {
return beansOfType.values().iterator().next();
}
else {
throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size());
}
}
Return a single bean of the given type or subtypes, not looking in ancestor
factories. Useful convenience method when we expect a single bean and
don't care about the bean name.
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). |
public static Object beanOfTypeIncludingAncestors(ListableBeanFactory lbf,
Class type) throws BeansException {
Map beansOfType = beansOfTypeIncludingAncestors(lbf, type);
if (beansOfType.size() == 1) {
return beansOfType.values().iterator().next();
}
else {
throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size());
}
}
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. Useful convenience method when we expect a
single bean and don't care about the bean name.
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. |
public static Object beanOfTypeIncludingAncestors(ListableBeanFactory lbf,
Class type,
boolean includeNonSingletons,
boolean allowEagerInit) throws BeansException {
Map beansOfType = beansOfTypeIncludingAncestors(lbf, type, includeNonSingletons, allowEagerInit);
if (beansOfType.size() == 1) {
return beansOfType.values().iterator().next();
}
else {
throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size());
}
}
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. Useful convenience method when we expect a
single bean and don't care about the bean name.
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). |
public static Map beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
Class type) throws BeansException {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
Map result = new LinkedHashMap(4);
result.putAll(lbf.getBeansOfType(type));
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
Map parentResult = beansOfTypeIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), type);
for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String beanName = (String) entry.getKey();
if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) {
result.put(beanName, entry.getValue());
}
}
}
}
return result;
}
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.
The returned Map will only contain beans of this 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. |
public static Map beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
Class type,
boolean includeNonSingletons,
boolean allowEagerInit) throws BeansException {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
Map result = new LinkedHashMap(4);
result.putAll(lbf.getBeansOfType(type, includeNonSingletons, allowEagerInit));
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
Map parentResult = beansOfTypeIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit);
for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String beanName = (String) entry.getKey();
if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) {
result.put(beanName, entry.getValue());
}
}
}
}
return result;
}
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.
The returned Map will only contain beans of this type.
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). |
public static int countBeansIncludingAncestors(ListableBeanFactory lbf) {
return beanNamesIncludingAncestors(lbf).length;
}
Count all beans in any hierarchy in which this factory participates.
Includes counts of ancestor bean factories.
Beans that are "overridden" (specified in a descendant factory
with the same name) are only counted once. |
public static boolean isFactoryDereference(String name) {
return (name != null && name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));
}
Return whether the given name is a factory dereference
(beginning with the factory dereference prefix). |
public static boolean isGeneratedBeanName(String name) {
return (name != null && name.indexOf(GENERATED_BEAN_NAME_SEPARATOR) != -1);
}
Return whether the given name is a bean name which has been generated
by the default naming strategy (containing a "#..." part). |
public static String originalBeanName(String name) {
Assert.notNull(name, "'name' must not be null");
int separatorIndex = name.indexOf(GENERATED_BEAN_NAME_SEPARATOR);
return (separatorIndex != -1 ? name.substring(0, separatorIndex) : name);
}
Extract the "raw" bean name from the given (potentially generated) bean name,
excluding any "#..." suffixes which might have been added for uniqueness. |
public static String transformedBeanName(String name) {
Assert.notNull(name, "'name' must not be null");
String beanName = name;
while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
}
return beanName;
}
Return the actual bean name, stripping out the factory dereference
prefix (if any, also stripping repeated factory prefixes if found). |