Support for resource patterns in @PropertySource locations
Closes gh-21325
This commit is contained in:
parent
3228502108
commit
9c74c25961
|
|
@ -234,6 +234,14 @@ class PropertySourceAnnotationTests {
|
|||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipleResourcesFromPropertySourcePattern() { // gh-21325
|
||||
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ResourcePatternConfig.class);
|
||||
ctx.getBean(ResourcePatternConfig.class);
|
||||
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2", "from.p3", "from.p4", "from.p5");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withNamedPropertySources() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNamedPropertySources.class);
|
||||
|
|
@ -277,15 +285,15 @@ class PropertySourceAnnotationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
void orderingWithAndWithoutNameAndFourResourceLocations() {
|
||||
void orderingWithFourResourceLocations() {
|
||||
// SPR-12198: p4 should 'win' as it was registered last
|
||||
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
|
||||
assertEnvironmentProperty(ctxWithoutName, "testbean.name", "p4TestBean");
|
||||
ctxWithoutName.close();
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
|
||||
assertEnvironmentProperty(ctx, "testbean.name", "p4TestBean");
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void orderingDoesntReplaceExisting() throws Exception {
|
||||
void orderingDoesntReplaceExisting() {
|
||||
// SPR-12198: mySource should 'win' as it was registered manually
|
||||
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext();
|
||||
MapPropertySource mySource = new MapPropertySource("mine", Map.of("testbean.name", "myTestBean"));
|
||||
|
|
@ -522,6 +530,12 @@ class PropertySourceAnnotationTests {
|
|||
static class MultipleComposedAnnotationsConfig {
|
||||
}
|
||||
|
||||
|
||||
@PropertySource("classpath*:org/springframework/context/annotation/p?.properties")
|
||||
static class ResourcePatternConfig {
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@PropertySources({
|
||||
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ import org.springframework.util.ReflectionUtils;
|
|||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 6.0
|
||||
* @see PropertySourceDescriptor
|
||||
*/
|
||||
|
|
@ -58,14 +59,14 @@ public class PropertySourceProcessor {
|
|||
|
||||
private final ConfigurableEnvironment environment;
|
||||
|
||||
private final ResourceLoader resourceLoader;
|
||||
private final ResourcePatternResolver resourcePatternResolver;
|
||||
|
||||
private final List<String> propertySourceNames = new ArrayList<>();
|
||||
|
||||
|
||||
public PropertySourceProcessor(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
|
||||
this.environment = environment;
|
||||
this.resourceLoader = resourceLoader;
|
||||
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -87,8 +88,9 @@ public class PropertySourceProcessor {
|
|||
for (String location : locations) {
|
||||
try {
|
||||
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
|
||||
Resource resource = this.resourceLoader.getResource(resolvedLocation);
|
||||
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
|
||||
for (Resource resource : this.resourcePatternResolver.getResources(resolvedLocation)) {
|
||||
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
|
||||
}
|
||||
}
|
||||
catch (RuntimeException | IOException ex) {
|
||||
// Placeholders not resolvable (IllegalArgumentException) or resource not found when trying to open it
|
||||
|
|
|
|||
Loading…
Reference in New Issue