@Value values may use ${...} placeholders (driven by PropertyPlaceholderConfigurer); @Autowired uses field/parameter name as fallback qualifier value (SPR-5152)

This commit is contained in:
Juergen Hoeller
2009-03-31 20:27:42 +00:00
parent 2524ca33d5
commit b85d45725d
10 changed files with 184 additions and 30 deletions

View File

@@ -17,22 +17,22 @@
package org.springframework.beans.factory.xml;
import static java.lang.String.format;
import static org.junit.Assert.*;
import static org.springframework.util.ClassUtils.convertClassNameToResourcePath;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Properties;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.context.support.StaticApplicationContext;
import static org.springframework.util.ClassUtils.*;
/**
* @author Mark Fisher
@@ -86,6 +86,30 @@ public final class QualifierAnnotationTests {
assertTrue(testBean.myProps != null && testBean.myProps.isEmpty());
}
@Test
public void testQualifiedByFieldName() {
StaticApplicationContext context = new StaticApplicationContext();
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
reader.loadBeanDefinitions(CONFIG_LOCATION);
context.registerSingleton("testBean", QualifiedByFieldNameTestBean.class);
context.refresh();
QualifiedByFieldNameTestBean testBean = (QualifiedByFieldNameTestBean) context.getBean("testBean");
Person person = testBean.getLarry();
assertEquals("LarryBean", person.getName());
}
@Test
public void testQualifiedByParameterName() {
StaticApplicationContext context = new StaticApplicationContext();
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
reader.loadBeanDefinitions(CONFIG_LOCATION);
context.registerSingleton("testBean", QualifiedByParameterNameTestBean.class);
context.refresh();
QualifiedByParameterNameTestBean testBean = (QualifiedByParameterNameTestBean) context.getBean("testBean");
Person person = testBean.getLarry();
assertEquals("LarryBean", person.getName());
}
@Test
public void testQualifiedByAlias() {
StaticApplicationContext context = new StaticApplicationContext();
@@ -203,6 +227,32 @@ public final class QualifierAnnotationTests {
}
private static class QualifiedByFieldNameTestBean {
@Autowired
private Person larryBean;
public Person getLarry() {
return larryBean;
}
}
private static class QualifiedByParameterNameTestBean {
private Person larryBean;
@Autowired
public void setLarryBean(Person larryBean) {
this.larryBean = larryBean;
}
public Person getLarry() {
return larryBean;
}
}
private static class QualifiedByAliasTestBean {
@Autowired @Qualifier("stooge")

View File

@@ -16,6 +16,8 @@
package org.springframework.context.expression;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.junit.Assert.*;
@@ -26,6 +28,7 @@ import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.config.Scope;
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -70,6 +73,12 @@ public class ApplicationContextExpressionTests {
}
});
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Properties placeholders = new Properties();
placeholders.setProperty("code", "123");
ppc.setProperties(placeholders);
ac.addBeanFactoryPostProcessor(ppc);
GenericBeanDefinition bd0 = new GenericBeanDefinition();
bd0.setBeanClass(TestBean.class);
bd0.getPropertyValues().addPropertyValue("name", "myName");
@@ -88,7 +97,7 @@ public class ApplicationContextExpressionTests {
bd2.setScope("myScope");
bd2.getPropertyValues().addPropertyValue("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
bd2.getPropertyValues().addPropertyValue("age", "#{mySpecialAttr}");
bd2.getPropertyValues().addPropertyValue("country", "#{systemProperties.country}");
bd2.getPropertyValues().addPropertyValue("country", "${code} #{systemProperties.country}");
ac.registerBeanDefinition("tb2", bd2);
GenericBeanDefinition bd3 = new GenericBeanDefinition();
@@ -124,30 +133,30 @@ public class ApplicationContextExpressionTests {
TestBean tb2 = ac.getBean("tb2", TestBean.class);
assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
assertEquals(42, tb2.getAge());
assertEquals("UK", tb2.getCountry());
assertEquals("123 UK", tb2.getCountry());
ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
assertEquals(42, tb3.age);
assertEquals("UK", tb3.country);
assertEquals("123 UK", tb3.country);
assertSame(tb0, tb3.tb);
ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
assertEquals(42, tb4.age);
assertEquals("UK", tb4.country);
assertEquals("123 UK", tb4.country);
assertSame(tb0, tb4.tb);
MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
assertEquals(42, tb5.age);
assertEquals("UK", tb5.country);
assertEquals("123 UK", tb5.country);
assertSame(tb0, tb5.tb);
PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
assertEquals(42, tb6.age);
assertEquals("UK", tb6.country);
assertEquals("123 UK", tb6.country);
assertSame(tb0, tb6.tb);
}
finally {
@@ -197,7 +206,7 @@ public class ApplicationContextExpressionTests {
@Autowired @Value("#{mySpecialAttr}")
public int age;
@Value("#{systemProperties.country}")
@Value("${code} #{systemProperties.country}")
public String country;
@Qualifier("original")
@@ -218,9 +227,9 @@ public class ApplicationContextExpressionTests {
@Autowired
public ConstructorValueTestBean(
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
@Value("#{mySpecialAttr}")int age,
@Value("#{mySpecialAttr}") int age,
@Qualifier("original") TestBean tb,
@Value("#{systemProperties.country}") String country) {
@Value("${code} #{systemProperties.country}") String country) {
this.name = name;
this.age = age;
this.country = country;
@@ -244,7 +253,7 @@ public class ApplicationContextExpressionTests {
@Qualifier("original") TestBean tb,
@Value("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ") String name,
@Value("#{mySpecialAttr}") int age,
@Value("#{systemProperties.country}") String country) {
@Value("${code} #{systemProperties.country}") String country) {
this.name = name;
this.age = age;
this.country = country;
@@ -273,7 +282,7 @@ public class ApplicationContextExpressionTests {
this.age = age;
}
@Value("#{systemProperties.country}")
@Value("${code} #{systemProperties.country}")
public void setCountry(String country) {
this.country = country;
}