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

master
Juergen Hoeller 15 years ago
parent f3274624b3
commit bddb38d787
  1. 6
      org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  2. 35
      org.springframework.context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.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 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) {

@ -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 {

Loading…
Cancel
Save