Add @PropertySources and ignoreResourceNotFound

Support repeatable @PropertySource annotations in Java 8 and add
@PropertySources container annotation for Java 6/7. Also add an
ignoreResourceNotFound attribute to @PropertySource allowing missing
property resources to be silently ignored.

This commit also introduces some generally useful methods to
AnnotationUtils for working with @Repeatable annotations.

Issue: SPR-8371
This commit is contained in:
Phillip Webb
2013-10-22 11:10:20 -07:00
parent 8917821e95
commit e95bd9e250
10 changed files with 364 additions and 52 deletions

View File

@@ -16,29 +16,35 @@
package org.springframework.context.annotation;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.FileNotFoundException;
import java.util.Iterator;
import javax.inject.Inject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.tests.sample.beans.TestBean;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* Tests the processing of @PropertySource annotations on @Configuration classes.
*
* @author Chris Beams
* @author Phillip Webb
* @since 3.1
*/
public class PropertySourceAnnotationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void withExplicitName() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
@@ -149,6 +155,29 @@ public class PropertySourceAnnotationTests {
ctx.refresh();
}
@Test
public void withPropertySources() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithPropertySources.class);
assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
// p2 should 'win' as it was registered last
assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
}
@Test
public void withMissingPropertySource() {
thrown.expect(BeanDefinitionStoreException.class);
thrown.expectCause(isA(FileNotFoundException.class));
new AnnotationConfigApplicationContext(ConfigWithMissingPropertySource.class);
}
@Test
public void withIgnoredPropertySource() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithIgnoredPropertySource.class);
assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
}
@Configuration
@PropertySource(value="classpath:${unresolvable}/p1.properties")
@@ -231,6 +260,33 @@ public class PropertySourceAnnotationTests {
}
@Configuration
@PropertySources({
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p2.properties")
})
static class ConfigWithPropertySources {
}
@Configuration
@PropertySources({
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/missing.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p2.properties")
})
static class ConfigWithMissingPropertySource {
}
@Configuration
@PropertySources({
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/missing.properties", ignoreResourceNotFound=true),
@PropertySource(name = "psName", value="classpath:org/springframework/context/annotation/p2.properties")
})
static class ConfigWithIgnoredPropertySource {
}
@Configuration
@PropertySource(value = {})
static class ConfigWithEmptyResourceLocations {