Skip to content

Commit 54571bf

Browse files
committed
Introduced getBeanNamesForAnnotation method on ListableBeanFactory
Issue: SPR-11069
1 parent b43901e commit 54571bf

6 files changed

Lines changed: 78 additions & 9 deletions

File tree

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,36 @@ <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, b
212212
throws BeansException;
213213

214214
/**
215-
* Find all beans whose {@code Class} has the supplied {@link java.lang.annotation.Annotation} type.
215+
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
216+
* without creating any bean instances yet.
217+
* @param annotationType the type of annotation to look for
218+
* @return the names of all matching beans
219+
* @since 4.0
220+
*/
221+
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
222+
223+
/**
224+
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
225+
* returning a Map of bean names with corresponding bean instances.
216226
* @param annotationType the type of annotation to look for
217227
* @return a Map with the matching beans, containing the bean names as
218228
* keys and the corresponding bean instances as values
219229
* @throws BeansException if a bean could not be created
230+
* @since 3.0
220231
*/
221-
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
222-
throws BeansException;
232+
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
223233

224234
/**
225-
* Find a {@link Annotation} of {@code annotationType} on the specified
235+
* Find an {@link Annotation} of {@code annotationType} on the specified
226236
* bean, traversing its interfaces and super classes if no annotation can be
227237
* found on the given class itself.
228238
* @param beanName the name of the bean to look for annotations on
229239
* @param annotationType the annotation class to look for
230240
* @return the annotation of the given type found, or {@code null}
241+
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
242+
* @since 3.0
231243
*/
232-
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType);
244+
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
245+
throws NoSuchBeanDefinitionException;
233246

234247
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,23 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle
487487
return result;
488488
}
489489

490+
@Override
491+
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
492+
List<String> results = new ArrayList<String>();
493+
for (String beanName : getBeanDefinitionNames()) {
494+
BeanDefinition beanDefinition = getBeanDefinition(beanName);
495+
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
496+
results.add(beanName);
497+
}
498+
}
499+
for (String beanName : getSingletonNames()) {
500+
if (!results.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
501+
results.add(beanName);
502+
}
503+
}
504+
return results.toArray(new String[results.size()]);
505+
}
506+
490507
@Override
491508
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
492509
Map<String, Object> results = new LinkedHashMap<String, Object>();
@@ -511,7 +528,9 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
511528
* if not found on the exposed bean reference (e.g. in case of a proxy).
512529
*/
513530
@Override
514-
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
531+
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
532+
throws NoSuchBeanDefinitionException{
533+
515534
A ann = null;
516535
Class<?> beanType = getType(beanName);
517536
if (beanType != null) {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle
276276
return matches;
277277
}
278278

279+
@Override
280+
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
281+
List<String> results = new ArrayList<String>();
282+
for (String beanName : this.beans.keySet()) {
283+
if (findAnnotationOnBean(beanName, annotationType) != null) {
284+
results.add(beanName);
285+
}
286+
}
287+
return results.toArray(new String[results.size()]);
288+
}
289+
279290
@Override
280291
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
281292
throws BeansException {
@@ -290,7 +301,9 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
290301
}
291302

292303
@Override
293-
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
304+
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
305+
throws NoSuchBeanDefinitionException{
306+
294307
return AnnotationUtils.findAnnotation(getType(beanName), annotationType);
295308
}
296309

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
172172
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
173173
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
174174
assertEquals(0, beanNames.length);
175+
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
176+
assertEquals(0, beanNames.length);
177+
175178
assertFalse(lbf.containsSingleton("x1"));
176179
assertTrue(lbf.containsBean("x1"));
177180
assertTrue(lbf.containsBean("&x1"));
@@ -201,6 +204,9 @@ public void testPrototypeSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
201204
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
202205
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
203206
assertEquals(0, beanNames.length);
207+
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
208+
assertEquals(0, beanNames.length);
209+
204210
assertFalse(lbf.containsSingleton("x1"));
205211
assertTrue(lbf.containsBean("x1"));
206212
assertTrue(lbf.containsBean("&x1"));
@@ -229,6 +235,9 @@ public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
229235
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
230236
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
231237
assertEquals(0, beanNames.length);
238+
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
239+
assertEquals(0, beanNames.length);
240+
232241
assertFalse(lbf.containsSingleton("x1"));
233242
assertTrue(lbf.containsBean("x1"));
234243
assertTrue(lbf.containsBean("&x1"));

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,12 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle
10711071
return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit);
10721072
}
10731073

1074+
@Override
1075+
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
1076+
assertBeanFactoryActive();
1077+
return getBeanFactory().getBeanNamesForAnnotation(annotationType);
1078+
}
1079+
10741080
@Override
10751081
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
10761082
throws BeansException {
@@ -1080,7 +1086,9 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
10801086
}
10811087

10821088
@Override
1083-
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
1089+
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
1090+
throws NoSuchBeanDefinitionException{
1091+
10841092
assertBeanFactoryActive();
10851093
return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
10861094
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle
238238
return this.beanFactory.getBeansOfType(type, includeNonSingletons, allowEagerInit);
239239
}
240240

241+
@Override
242+
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
243+
return this.beanFactory.getBeanNamesForAnnotation(annotationType);
244+
}
245+
241246
@Override
242247
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
243248
throws BeansException {
@@ -246,7 +251,9 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
246251
}
247252

248253
@Override
249-
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
254+
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
255+
throws NoSuchBeanDefinitionException{
256+
250257
return this.beanFactory.findAnnotationOnBean(beanName, annotationType);
251258
}
252259

0 commit comments

Comments
 (0)