fixed regression: looking for annotation on original bean class as well, not just on exposed bean type (SPR-5981)
This commit is contained in:
parent
ad492e906e
commit
e2093a8414
|
|
@ -214,17 +214,16 @@ 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());
|
||||
}
|
||||
else if (bd.hasBeanClass()) {
|
||||
beanType = bd.getBeanClass();
|
||||
}
|
||||
Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName());
|
||||
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)) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue