Polishing

This commit is contained in:
Juergen Hoeller
2018-08-11 01:30:32 +02:00
parent 951b39cc7a
commit 67d0c69a95
2 changed files with 53 additions and 36 deletions

View File

@@ -106,6 +106,8 @@ public abstract class BeanFactoryUtils {
}
// Retrieval of bean names
/**
* Count all beans in any hierarchy in which this factory participates.
* Includes counts of ancestor bean factories.
@@ -113,6 +115,7 @@ public abstract class BeanFactoryUtils {
* with the same name) are only counted once.
* @param lbf the bean factory
* @return count of beans including those defined in ancestor factories
* @see #beanNamesIncludingAncestors
*/
public static int countBeansIncludingAncestors(ListableBeanFactory lbf) {
return beanNamesIncludingAncestors(lbf).length;
@@ -140,6 +143,7 @@ public abstract class BeanFactoryUtils {
* @param type the type that beans must match (as a {@code ResolvableType})
* @return the array of matching bean names, or an empty array if none
* @since 4.2
* @see ListableBeanFactory#getBeanNamesForType(ResolvableType)
*/
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, ResolvableType type) {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
@@ -166,6 +170,7 @@ public abstract class BeanFactoryUtils {
* @param lbf the bean factory
* @param type the type that beans must match (as a {@code Class})
* @return the array of matching bean names, or an empty array if none
* @see ListableBeanFactory#getBeanNamesForType(Class)
*/
public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type) {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
@@ -200,6 +205,7 @@ public abstract class BeanFactoryUtils {
* for this flag will initialize FactoryBeans and "factory-bean" references.
* @param type the type that beans must match
* @return the array of matching bean names, or an empty array if none
* @see ListableBeanFactory#getBeanNamesForType(Class, boolean, boolean)
*/
public static String[] beanNamesForTypeIncludingAncestors(
ListableBeanFactory lbf, Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
@@ -217,6 +223,35 @@ public abstract class BeanFactoryUtils {
return result;
}
/**
* Get all bean names whose {@code Class} has the supplied {@link Annotation}
* type, including those defined in ancestor factories, without creating any bean
* instances yet. Will return unique names in case of overridden bean definitions.
* @param lbf the bean factory
* @param annotationType the type of annotation to look for
* @return the array of matching bean names, or an empty array if none
* @since 5.0
* @see ListableBeanFactory#getBeanNamesForAnnotation(Class)
*/
public static String[] beanNamesForAnnotationIncludingAncestors(
ListableBeanFactory lbf, Class<? extends Annotation> annotationType) {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
String[] result = lbf.getBeanNamesForAnnotation(annotationType);
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
String[] parentResult = beanNamesForAnnotationIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), annotationType);
result = mergeNamesWithParent(result, parentResult, hbf);
}
}
return result;
}
// Retrieval of bean instances
/**
* 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.
@@ -233,6 +268,7 @@ public abstract class BeanFactoryUtils {
* @param type type of bean to match
* @return the Map of matching bean instances, or an empty Map if none
* @throws BeansException if a bean could not be created
* @see ListableBeanFactory#getBeansOfType(Class)
*/
public static <T> Map<String, T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type)
throws BeansException {
@@ -280,6 +316,7 @@ public abstract class BeanFactoryUtils {
* 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
* @see ListableBeanFactory#getBeansOfType(Class, boolean, boolean)
*/
public static <T> Map<String, T> beansOfTypeIncludingAncestors(
ListableBeanFactory lbf, Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
@@ -303,7 +340,6 @@ public abstract class BeanFactoryUtils {
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
@@ -325,6 +361,7 @@ public abstract class BeanFactoryUtils {
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @see #beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
*/
public static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type)
throws BeansException {
@@ -333,31 +370,6 @@ public abstract class BeanFactoryUtils {
return uniqueBean(type, beansOfType);
}
/**
* Get all bean names whose {@code Class} has the supplied {@link Annotation}
* type, including those defined in ancestor factories, without creating any bean
* instances yet. Will return unique names in case of overridden bean definitions.
* @param lbf the bean factory
* @param annotationType the type of annotation to look for
* @return the array of matching bean names, or an empty array if none
* @since 5.0
*/
public static String[] beanNamesForAnnotationIncludingAncestors(
ListableBeanFactory lbf, Class<? extends Annotation> annotationType) {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
String[] result = lbf.getBeanNamesForAnnotation(annotationType);
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
String[] parentResult = beanNamesForAnnotationIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), annotationType);
result = mergeNamesWithParent(result, parentResult, hbf);
}
}
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
@@ -386,6 +398,7 @@ public abstract class BeanFactoryUtils {
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @see #beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
*/
public static <T> T beanOfTypeIncludingAncestors(
ListableBeanFactory lbf, Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
@@ -410,6 +423,7 @@ public abstract class BeanFactoryUtils {
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @see ListableBeanFactory#getBeansOfType(Class)
*/
public static <T> T beanOfType(ListableBeanFactory lbf, Class<T> type) throws BeansException {
Assert.notNull(lbf, "ListableBeanFactory must not be null");
@@ -440,6 +454,7 @@ public abstract class BeanFactoryUtils {
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @see ListableBeanFactory#getBeansOfType(Class, boolean, boolean)
*/
public static <T> T beanOfType(
ListableBeanFactory lbf, Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)