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 c8ab1fc70e..707a907d32 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 @@ -425,7 +425,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto * if not found on the exposed bean reference (e.g. in case of a proxy). */ public A findAnnotationOnBean(String beanName, Class annotationType) { - A ann = AnnotationUtils.findAnnotation(getType(beanName), annotationType); + A ann = null; + Class beanType = getType(beanName); + if (beanType != null) { + ann = AnnotationUtils.findAnnotation(beanType, annotationType); + } if (ann == null && containsBeanDefinition(beanName)) { BeanDefinition bd = getMergedBeanDefinition(beanName); if (bd instanceof AbstractBeanDefinition) { diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java index dcdb918d9b..7f38354d02 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java @@ -17,22 +17,24 @@ package org.springframework.context.annotation; import static java.lang.String.format; +import java.util.Map; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import org.junit.Test; import static org.junit.matchers.JUnitMatchers.*; -import static org.springframework.util.StringUtils.uncapitalize; +import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation6.ComponentForScanning; import org.springframework.context.annotation6.ConfigForScanning; import org.springframework.context.annotation6.Jsr330NamedForScanning; -import org.springframework.util.StringUtils; +import static org.springframework.util.StringUtils.*; /** * @author Chris Beams + * @author Juergen Hoeller */ public class AnnotationConfigApplicationContextTests { @@ -51,6 +53,8 @@ public class AnnotationConfigApplicationContextTests { context.getBean("testBean"); // contributed by ConfigForScanning context.getBean(uncapitalize(ComponentForScanning.class.getSimpleName())); context.getBean(uncapitalize(Jsr330NamedForScanning.class.getSimpleName())); + Map beans = context.getBeansWithAnnotation(Configuration.class); + assertEquals(1, beans.size()); } @Test @@ -60,6 +64,19 @@ public class AnnotationConfigApplicationContextTests { context.refresh(); context.getBean("testBean"); context.getBean("name"); + Map beans = context.getBeansWithAnnotation(Configuration.class); + assertEquals(2, beans.size()); + } + + @Test + public void getBeansWithAnnotation() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(Config.class, NameConfig.class, UntypedFactoryBean.class); + context.refresh(); + context.getBean("testBean"); + context.getBean("name"); + Map beans = context.getBeansWithAnnotation(Configuration.class); + assertEquals(2, beans.size()); } @Test @@ -186,6 +203,20 @@ public class AnnotationConfigApplicationContextTests { } } + static class UntypedFactoryBean implements FactoryBean { + + public Object getObject() { + return null; + } + + public Class getObjectType() { + return null; + } + + public boolean isSingleton() { + return false; + } + } } class TestBean {