提交 e2093a84 编写于 作者: J Juergen Hoeller

fixed regression: looking for annotation on original bean class as well, not...

fixed regression: looking for annotation on original bean class as well, not just on exposed bean type (SPR-5981)
上级 ad492e90
......@@ -214,15 +214,14 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan
}
if (targetAnnotation == null) {
// look for matching annotation on the target class
Class<?> beanType = null;
if (this.beanFactory != null) {
beanType = this.beanFactory.getType(bdHolder.getBeanName());
Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
if (beanType != null) {
targetAnnotation = ClassUtils.getUserClass(beanType).getAnnotation(type);
}
}
else if (bd.hasBeanClass()) {
beanType = bd.getBeanClass();
}
if (beanType != null) {
targetAnnotation = ClassUtils.getUserClass(beanType).getAnnotation(type);
if (targetAnnotation == null && bd.hasBeanClass()) {
targetAnnotation = ClassUtils.getUserClass(bd.getBeanClass()).getAnnotation(type);
}
}
if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
......
......@@ -45,4 +45,10 @@
<bean id="testProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"/>
<bean id="thetaClient" class="org.springframework.beans.factory.xml.QualifierAnnotationTests$MultiQualifierClient"/>
<bean id="thetaFactory" class="org.springframework.beans.factory.xml.QualifierAnnotationTests$QualifiedFactoryBean"/>
<bean id="thetaImpl" class="org.springframework.beans.factory.xml.QualifierAnnotationTests$ThetaImpl"/>
</beans>
......@@ -27,6 +27,7 @@ import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
......@@ -181,13 +182,20 @@ public final class QualifierAnnotationTests {
QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver)
context.getDefaultListableBeanFactory().getAutowireCandidateResolver();
resolver.addQualifierType(MultipleAttributeQualifier.class);
context.registerSingleton("testBean", QualifiedByAttributesTestBean.class);
context.registerSingleton("testBean", MultiQualifierClient.class);
context.refresh();
QualifiedByAttributesTestBean testBean = (QualifiedByAttributesTestBean) context.getBean("testBean");
Person moeSenior = testBean.getMoeSenior();
Person moeJunior = testBean.getMoeJunior();
assertEquals("Moe Sr.", moeSenior.getName());
assertEquals("Moe Jr.", moeJunior.getName());
MultiQualifierClient testBean = (MultiQualifierClient) context.getBean("testBean");
assertNotNull( testBean.factoryTheta);
assertNotNull( testBean.implTheta);
}
@Test
public void testInterfaceWithOneQualifiedFactoryAndOneQualifiedBean() {
StaticApplicationContext context = new StaticApplicationContext();
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
reader.loadBeanDefinitions(CONFIG_LOCATION);
}
......@@ -353,4 +361,45 @@ public final class QualifierAnnotationTests {
int age();
}
private static final String FACTORY_QUALIFIER = "FACTORY";
private static final String IMPL_QUALIFIER = "IMPL";
public static class MultiQualifierClient {
@Autowired @Qualifier(FACTORY_QUALIFIER)
public Theta factoryTheta;
@Autowired @Qualifier(IMPL_QUALIFIER)
public Theta implTheta;
}
public interface Theta {
}
@Qualifier(IMPL_QUALIFIER)
public static class ThetaImpl implements Theta {
}
@Qualifier(FACTORY_QUALIFIER)
public static class QualifiedFactoryBean implements FactoryBean<Theta> {
public Theta getObject() {
return new Theta() {};
}
public Class<Theta> getObjectType() {
return Theta.class;
}
public boolean isSingleton() {
return true;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册