From d7a16303817d3735cdad277c2202a583ba146463 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 13 Dec 2009 23:23:34 +0000 Subject: [PATCH] removed getBeansWithAnnotation(Class,boolean,boolean) method from ListableBeanFactory; reimplemented getBeansWithAnnotation(Class) to avoid use of getBeanNamesForType(Object.class) --- .../beans/factory/ListableBeanFactory.java | 20 +------------------ .../support/DefaultListableBeanFactory.java | 13 ++++++------ .../support/StaticListableBeanFactory.java | 6 ------ 3 files changed, 7 insertions(+), 32 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java index 98450d305dd..4061d7f923d 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -221,24 +221,6 @@ $ *

Does not consider any hierarchy this factory may participate in. Map getBeansWithAnnotation(Class annotationType) throws BeansException; - /** - * Find all beans whose Class has the supplied {@link java.lang.annotation.Annotation} type. - * @param annotationType the type of annotation to look for - * @return a Map with the matching beans, containing the bean names as - * keys and the corresponding bean instances as values - * @param includeNonSingletons whether to include prototype or scoped beans too - * or just singletons (also applies to FactoryBeans) - * @param 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. - * @throws BeansException if a bean could not be created - */ - Map getBeansWithAnnotation( - Class annotationType, boolean includeNonSingletons, boolean allowEagerInit) - throws BeansException; - /** * Find a {@link Annotation} of annotationType on the specified * bean, traversing its interfaces and super classes if no annotation can be diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 3863c6b48de..c8ab1fc70e8 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -26,9 +26,11 @@ import java.lang.reflect.Type; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -404,14 +406,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto } public Map getBeansWithAnnotation(Class annotationType) { - return getBeansWithAnnotation(annotationType, true, true); - } - - public Map getBeansWithAnnotation( - Class annotationType, boolean includeNonSingletons, boolean allowEagerInit) { - + Set beanNames = new LinkedHashSet(getBeanDefinitionCount()); + beanNames.addAll(Arrays.asList(getBeanDefinitionNames())); + beanNames.addAll(Arrays.asList(getSingletonNames())); Map results = new LinkedHashMap(); - for (String beanName : getBeanNamesForType(Object.class, includeNonSingletons, allowEagerInit)) { + for (String beanName : beanNames) { if (findAnnotationOnBean(beanName, annotationType) != null) { results.put(beanName, getBean(beanName)); } diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java index 271356b0706..3e8784b1817 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java @@ -258,12 +258,6 @@ public class StaticListableBeanFactory implements ListableBeanFactory { public Map getBeansWithAnnotation(Class annotationType) throws BeansException { - return getBeansWithAnnotation(annotationType, true, true); - } - - public Map getBeansWithAnnotation( - Class annotationType, boolean includeNonSingletons, boolean allowEagerInit) { - Map results = new LinkedHashMap(); for (String beanName : this.beans.keySet()) { if (findAnnotationOnBean(beanName, annotationType) != null) {