@Value can be used as aliased meta-annotation

Issue: SPR-13603
This commit is contained in:
Juergen Hoeller
2015-12-29 18:02:16 +01:00
parent 3242ad8fc4
commit 4f955932a7
3 changed files with 123 additions and 13 deletions

View File

@@ -17,6 +17,8 @@
package org.springframework.context.annotation.configuration;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Optional;
import javax.inject.Provider;
@@ -35,6 +37,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.tests.sample.beans.Colour;
@@ -119,6 +122,20 @@ public class AutowiredConfigurationTests {
doTestValueInjection(context);
}
@Test
public void testValueInjectionWithMetaAnnotation() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ValueConfigWithMetaAnnotation.class);
doTestValueInjection(context);
}
@Test
public void testValueInjectionWithAliasedMetaAnnotation() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ValueConfigWithAliasedMetaAnnotation.class);
doTestValueInjection(context);
}
@Test
public void testValueInjectionWithProviderFields() {
AnnotationConfigApplicationContext context =
@@ -291,6 +308,73 @@ public class AutowiredConfigurationTests {
}
@Value("#{systemProperties[myProp]}")
@Retention(RetentionPolicy.RUNTIME)
public @interface MyProp {
}
@Configuration
@Scope("prototype")
static class ValueConfigWithMetaAnnotation {
@MyProp
private String name;
private String name2;
@MyProp
public void setName2(String name) {
this.name2 = name;
}
@Bean @Scope("prototype")
public TestBean testBean() {
return new TestBean(name);
}
@Bean @Scope("prototype")
public TestBean testBean2() {
return new TestBean(name2);
}
}
@Value("")
@Retention(RetentionPolicy.RUNTIME)
public @interface AliasedProp {
@AliasFor(annotation = Value.class)
String value();
}
@Configuration
@Scope("prototype")
static class ValueConfigWithAliasedMetaAnnotation {
@AliasedProp("#{systemProperties[myProp]}")
private String name;
private String name2;
@AliasedProp("#{systemProperties[myProp]}")
public void setName2(String name) {
this.name2 = name;
}
@Bean @Scope("prototype")
public TestBean testBean() {
return new TestBean(name);
}
@Bean @Scope("prototype")
public TestBean testBean2() {
return new TestBean(name2);
}
}
@Configuration
static class ValueConfigWithProviderFields {