Browse Source

Introduced getBeanNamesForAnnotation method on ListableBeanFactory

Issue: SPR-11069
pull/407/merge
Juergen Hoeller 13 years ago
parent
commit
54571bf038
  1. 23
      spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java
  2. 21
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  3. 15
      spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java
  4. 9
      spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
  5. 10
      spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
  6. 9
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

23
spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java

@ -212,23 +212,36 @@ public interface ListableBeanFactory extends BeanFactory {
throws BeansException; throws BeansException;
/** /**
* Find all beans whose {@code Class} has the supplied {@link java.lang.annotation.Annotation} type. * Find all beans whose {@code Class} has the supplied {@link Annotation} type,
* without creating any bean instances yet.
* @param annotationType the type of annotation to look for
* @return the names of all matching beans
* @since 4.0
*/
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
/**
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
* returning a Map of bean names with corresponding bean instances.
* @param annotationType the type of annotation to look for * @param annotationType the type of annotation to look for
* @return a Map with the matching beans, containing the bean names as * @return a Map with the matching beans, containing the bean names as
* keys and the corresponding bean instances as values * keys and the corresponding bean instances as values
* @throws BeansException if a bean could not be created * @throws BeansException if a bean could not be created
* @since 3.0
*/ */
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
throws BeansException;
/** /**
* Find a {@link Annotation} of {@code annotationType} on the specified * Find an {@link Annotation} of {@code annotationType} on the specified
* bean, traversing its interfaces and super classes if no annotation can be * bean, traversing its interfaces and super classes if no annotation can be
* found on the given class itself. * found on the given class itself.
* @param beanName the name of the bean to look for annotations on * @param beanName the name of the bean to look for annotations on
* @param annotationType the annotation class to look for * @param annotationType the annotation class to look for
* @return the annotation of the given type found, or {@code null} * @return the annotation of the given type found, or {@code null}
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
* @since 3.0
*/ */
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType); <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException;
} }

21
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

@ -487,6 +487,23 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return result; return result;
} }
@Override
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
List<String> results = new ArrayList<String>();
for (String beanName : getBeanDefinitionNames()) {
BeanDefinition beanDefinition = getBeanDefinition(beanName);
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
results.add(beanName);
}
}
for (String beanName : getSingletonNames()) {
if (!results.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
results.add(beanName);
}
}
return results.toArray(new String[results.size()]);
}
@Override @Override
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) { public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
Map<String, Object> results = new LinkedHashMap<String, Object>(); Map<String, Object> results = new LinkedHashMap<String, Object>();
@ -511,7 +528,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
* if not found on the exposed bean reference (e.g. in case of a proxy). * if not found on the exposed bean reference (e.g. in case of a proxy).
*/ */
@Override @Override
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) { public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException{
A ann = null; A ann = null;
Class<?> beanType = getType(beanName); Class<?> beanType = getType(beanName);
if (beanType != null) { if (beanType != null) {

15
spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java

@ -276,6 +276,17 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
return matches; return matches;
} }
@Override
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
List<String> results = new ArrayList<String>();
for (String beanName : this.beans.keySet()) {
if (findAnnotationOnBean(beanName, annotationType) != null) {
results.add(beanName);
}
}
return results.toArray(new String[results.size()]);
}
@Override @Override
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
throws BeansException { throws BeansException {
@ -290,7 +301,9 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
} }
@Override @Override
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) { public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException{
return AnnotationUtils.findAnnotation(getType(beanName), annotationType); return AnnotationUtils.findAnnotation(getType(beanName), annotationType);
} }

9
spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

@ -172,6 +172,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
assertEquals(0, beanNames.length); assertEquals(0, beanNames.length);
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
assertEquals(0, beanNames.length);
assertFalse(lbf.containsSingleton("x1")); assertFalse(lbf.containsSingleton("x1"));
assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("x1"));
assertTrue(lbf.containsBean("&x1")); assertTrue(lbf.containsBean("&x1"));
@ -201,6 +204,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
assertEquals(0, beanNames.length); assertEquals(0, beanNames.length);
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
assertEquals(0, beanNames.length);
assertFalse(lbf.containsSingleton("x1")); assertFalse(lbf.containsSingleton("x1"));
assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("x1"));
assertTrue(lbf.containsBean("&x1")); assertTrue(lbf.containsBean("&x1"));
@ -229,6 +235,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
assertEquals(0, beanNames.length); assertEquals(0, beanNames.length);
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
assertEquals(0, beanNames.length);
assertFalse(lbf.containsSingleton("x1")); assertFalse(lbf.containsSingleton("x1"));
assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("x1"));
assertTrue(lbf.containsBean("&x1")); assertTrue(lbf.containsBean("&x1"));

10
spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

@ -1071,6 +1071,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit); return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit);
} }
@Override
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForAnnotation(annotationType);
}
@Override @Override
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
throws BeansException { throws BeansException {
@ -1080,7 +1086,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
} }
@Override @Override
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) { public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException{
assertBeanFactoryActive(); assertBeanFactoryActive();
return getBeanFactory().findAnnotationOnBean(beanName, annotationType); return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
} }

9
spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

@ -238,6 +238,11 @@ class StubWebApplicationContext implements WebApplicationContext {
return this.beanFactory.getBeansOfType(type, includeNonSingletons, allowEagerInit); return this.beanFactory.getBeansOfType(type, includeNonSingletons, allowEagerInit);
} }
@Override
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
return this.beanFactory.getBeanNamesForAnnotation(annotationType);
}
@Override @Override
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
throws BeansException { throws BeansException {
@ -246,7 +251,9 @@ class StubWebApplicationContext implements WebApplicationContext {
} }
@Override @Override
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) { public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException{
return this.beanFactory.findAnnotationOnBean(beanName, annotationType); return this.beanFactory.findAnnotationOnBean(beanName, annotationType);
} }

Loading…
Cancel
Save