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,15 +214,14 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan
|
||||||
}
|
}
|
||||||
if (targetAnnotation == null) {
|
if (targetAnnotation == null) {
|
||||||
// look for matching annotation on the target class
|
// look for matching annotation on the target class
|
||||||
Class<?> beanType = null;
|
|
||||||
if (this.beanFactory != 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()) {
|
if (targetAnnotation == null && bd.hasBeanClass()) {
|
||||||
beanType = bd.getBeanClass();
|
targetAnnotation = ClassUtils.getUserClass(bd.getBeanClass()).getAnnotation(type);
|
||||||
}
|
|
||||||
if (beanType != null) {
|
|
||||||
targetAnnotation = ClassUtils.getUserClass(beanType).getAnnotation(type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
|
if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
|
||||||
|
|
|
||||||
|
|
@ -45,4 +45,10 @@
|
||||||
|
|
||||||
<bean id="testProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"/>
|
<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>
|
</beans>
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
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.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
|
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
|
||||||
|
|
@ -181,13 +182,20 @@ public final class QualifierAnnotationTests {
|
||||||
QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver)
|
QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver)
|
||||||
context.getDefaultListableBeanFactory().getAutowireCandidateResolver();
|
context.getDefaultListableBeanFactory().getAutowireCandidateResolver();
|
||||||
resolver.addQualifierType(MultipleAttributeQualifier.class);
|
resolver.addQualifierType(MultipleAttributeQualifier.class);
|
||||||
context.registerSingleton("testBean", QualifiedByAttributesTestBean.class);
|
context.registerSingleton("testBean", MultiQualifierClient.class);
|
||||||
context.refresh();
|
context.refresh();
|
||||||
QualifiedByAttributesTestBean testBean = (QualifiedByAttributesTestBean) context.getBean("testBean");
|
|
||||||
Person moeSenior = testBean.getMoeSenior();
|
MultiQualifierClient testBean = (MultiQualifierClient) context.getBean("testBean");
|
||||||
Person moeJunior = testBean.getMoeJunior();
|
|
||||||
assertEquals("Moe Sr.", moeSenior.getName());
|
assertNotNull( testBean.factoryTheta);
|
||||||
assertEquals("Moe Jr.", moeJunior.getName());
|
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();
|
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