📄 beanfactoryutils.java
字号:
Map result = new HashMap();
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();
if (!result.containsKey(entry.getKey())) {
result.put(entry.getKey(), 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.
* <p>Does consider objects created by FactoryBeans if the "includeFactoryBeans"
* 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 "includeFactoryBeans" is not set,
* only raw FactoryBeans will be checked (which doesn't require initialization
* of each FactoryBean).
* @param lbf the bean factory
* @param type type of bean to match
* @param includePrototypes whether to include prototype beans too or just singletons
* (also applies to FactoryBeans)
* @param includeFactoryBeans whether to include <i>objects created by
* FactoryBeans</i> (or by factory methods with a "factory-bean" reference)
* too, or just conventional beans. Note that FactoryBeans need to be
* initialized to determine their type: So be aware that passing in "true"
* for this flag will initialize FactoryBeans (and "factory-bean" references).
* @return the Map of matching bean instances, or an empty Map if none
* @throws BeansException if a bean could not be created
*/
public static Map beansOfTypeIncludingAncestors(
ListableBeanFactory lbf, Class type, boolean includePrototypes, boolean includeFactoryBeans)
throws BeansException {
Map result = new HashMap();
result.putAll(lbf.getBeansOfType(type, includePrototypes, includeFactoryBeans));
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
Map parentResult = beansOfTypeIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), type, includePrototypes, includeFactoryBeans);
for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
if (!result.containsKey(entry.getKey())) {
result.put(entry.getKey(), entry.getValue());
}
}
}
}
return result;
}
/**
* 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.
* <p>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.
* <p>This version of <code>beanOfTypeIncludingAncestors</code> automatically includes
* prototypes and FactoryBeans.
* @param lbf the bean factory
* @param type type of bean to match
* @return the matching bean instance
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if 0 or more than 1 beans of the given type were found
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if no single bean could be found for the given type
* @throws BeansException if the bean could not be created
*/
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.
* <p>Does consider objects created by FactoryBeans if the "includeFactoryBeans"
* 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 "includeFactoryBeans" is not set,
* only raw FactoryBeans will be checked (which doesn't require initialization
* of each FactoryBean).
* @param lbf the bean factory
* @param type type of bean to match
* @param includePrototypes whether to include prototype beans too or just singletons
* (also applies to FactoryBeans)
* @param includeFactoryBeans whether to include <i>objects created by
* FactoryBeans</i> (or by factory methods with a "factory-bean" reference)
* too, or just conventional beans. Note that FactoryBeans need to be
* initialized to determine their type: So be aware that passing in "true"
* for this flag will initialize FactoryBeans (and "factory-bean" references).
* @return the matching bean instance
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if 0 or more than 1 beans of the given type were found
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if no single bean could be found for the given type
* @throws BeansException if the bean could not be created
*/
public static Object beanOfTypeIncludingAncestors(
ListableBeanFactory lbf, Class type, boolean includePrototypes, boolean includeFactoryBeans)
throws BeansException {
Map beansOfType = beansOfTypeIncludingAncestors(lbf, type, includePrototypes, includeFactoryBeans);
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.
* <p>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.
* <p>This version of <code>beanOfType</code> automatically includes
* prototypes and FactoryBeans.
* @param lbf the bean factory
* @param type type of bean to match
* @return the matching bean instance
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if 0 or more than 1 beans of the given type were found
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if no single bean could be found for the given type
* @throws BeansException if the bean could not be created
*/
public static Object beanOfType(ListableBeanFactory lbf, Class type) throws BeansException {
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.
* <p>Does consider objects created by FactoryBeans if the "includeFactoryBeans"
* 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 "includeFactoryBeans" is not set,
* only raw FactoryBeans will be checked (which doesn't require initialization
* of each FactoryBean).
* @param lbf the bean factory
* @param type type of bean to match
* @param includePrototypes whether to include prototype beans too or just singletons
* (also applies to FactoryBeans)
* @param includeFactoryBeans whether to include <i>objects created by
* FactoryBeans</i> (or by factory methods with a "factory-bean" reference)
* too, or just conventional beans. Note that FactoryBeans need to be
* initialized to determine their type: So be aware that passing in "true"
* for this flag will initialize FactoryBeans (and "factory-bean" references).
* @return the matching bean instance
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if 0 or more than 1 beans of the given type were found
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if no single bean could be found for the given type
* @throws BeansException if the bean could not be created
*/
public static Object beanOfType(
ListableBeanFactory lbf, Class type, boolean includePrototypes, boolean includeFactoryBeans)
throws BeansException {
Map beansOfType = lbf.getBeansOfType(type, includePrototypes, includeFactoryBeans);
if (beansOfType.size() == 1) {
return beansOfType.values().iterator().next();
}
else {
throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -