scoped proxies retain original qualifiers (SPR-5911)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1574 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2009-07-21 13:00:00 +00:00
parent a313520265
commit cdf1c8d1a3
2 changed files with 41 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.aop.scope;
import org.springframework.aop.framework.autoproxy.AutoProxyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
@ -56,7 +57,7 @@ public abstract class ScopedProxyUtils {
scopedProxyDefinition.setOriginatingBeanDefinition(definition.getBeanDefinition());
scopedProxyDefinition.setSource(definition.getSource());
scopedProxyDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
String targetBeanName = getTargetBeanName(originalBeanName);
scopedProxyDefinition.getPropertyValues().addPropertyValue("targetBeanName", targetBeanName);
@ -68,9 +69,15 @@ public abstract class ScopedProxyUtils {
scopedProxyDefinition.getPropertyValues().addPropertyValue("proxyTargetClass", Boolean.FALSE);
}
// Copy autowire settings from original bean definition.
if (targetDefinition instanceof AbstractBeanDefinition) {
scopedProxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
}
scopedProxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
scopedProxyDefinition.setPrimary(targetDefinition.isPrimary());
// The target bean should be ignored in favor of the scoped proxy.
targetDefinition.setAutowireCandidate(false);
targetDefinition.setPrimary(false);
// Register the target bean as separate bean in the factory.
registry.registerBeanDefinition(targetBeanName, targetDefinition);

View File

@ -16,19 +16,21 @@
package org.springframework.beans.factory.support;
import static org.junit.Assert.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;
@ -145,7 +147,28 @@ public class QualifierAnnotationAutowireContextTests {
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
cavs.addGenericArgumentValue(JUERGEN);
RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
context.registerBeanDefinition(JUERGEN,
ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
context.registerBeanDefinition("autowired",
new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
context.refresh();
QualifiedMethodParameterTestBean bean =
(QualifiedMethodParameterTestBean) context.getBean("autowired");
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredMethodParameterWithStaticallyQualifiedCandidateAmongOthers() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
cavs.addGenericArgumentValue(JUERGEN);
RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
cavs2.addGenericArgumentValue(MARK);
RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
context.registerBeanDefinition(JUERGEN, person);
context.registerBeanDefinition(MARK, person2);
context.registerBeanDefinition("autowired",
new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
@ -657,13 +680,13 @@ public class QualifierAnnotationAutowireContextTests {
private static class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
@ -673,6 +696,10 @@ public class QualifierAnnotationAutowireContextTests {
@TestQualifier
private static class QualifiedPerson extends Person {
public QualifiedPerson() {
super(null);
}
public QualifiedPerson(String name) {
super(name);
}
@ -692,7 +719,6 @@ public class QualifierAnnotationAutowireContextTests {
public static @interface TestQualifierWithDefaultValue {
String value() default "default";
}
@ -704,7 +730,6 @@ public class QualifierAnnotationAutowireContextTests {
String value() default "default";
int number();
}
}