fixed getBeansWithAnnotation to ignore beans with non-determinable type (SPR-6579)

This commit is contained in:
Juergen Hoeller 2009-12-23 19:11:19 +00:00
parent f3274624b3
commit bddb38d787
2 changed files with 38 additions and 3 deletions

View File

@ -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 extends Annotation> A findAnnotationOnBean(String beanName, Class<A> 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) {

View File

@ -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<String, Object> 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<String, Object> 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<String, Object> 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 {