Skip to content

Commit 9c74c25

Browse files
committed
Support for resource patterns in @propertysource locations
Closes gh-21325
1 parent 3228502 commit 9c74c25

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

Diff for: spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ void multipleComposedPropertySourceAnnotations() { // gh-30941
234234
ctx.close();
235235
}
236236

237+
@Test
238+
void multipleResourcesFromPropertySourcePattern() { // gh-21325
239+
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ResourcePatternConfig.class);
240+
ctx.getBean(ResourcePatternConfig.class);
241+
assertEnvironmentContainsProperties(ctx, "from.p1", "from.p2", "from.p3", "from.p4", "from.p5");
242+
ctx.close();
243+
}
244+
237245
@Test
238246
void withNamedPropertySources() {
239247
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNamedPropertySources.class);
@@ -277,15 +285,15 @@ void orderingWithAndWithoutNameAndMultipleResourceLocations() {
277285
}
278286

279287
@Test
280-
void orderingWithAndWithoutNameAndFourResourceLocations() {
288+
void orderingWithFourResourceLocations() {
281289
// SPR-12198: p4 should 'win' as it was registered last
282-
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
283-
assertEnvironmentProperty(ctxWithoutName, "testbean.name", "p4TestBean");
284-
ctxWithoutName.close();
290+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
291+
assertEnvironmentProperty(ctx, "testbean.name", "p4TestBean");
292+
ctx.close();
285293
}
286294

287295
@Test
288-
void orderingDoesntReplaceExisting() throws Exception {
296+
void orderingDoesntReplaceExisting() {
289297
// SPR-12198: mySource should 'win' as it was registered manually
290298
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext();
291299
MapPropertySource mySource = new MapPropertySource("mine", Map.of("testbean.name", "myTestBean"));
@@ -522,6 +530,12 @@ static class ConfigWithRepeatedPropertySourceAnnotationsOnComposedAnnotation {
522530
static class MultipleComposedAnnotationsConfig {
523531
}
524532

533+
534+
@PropertySource("classpath*:org/springframework/context/annotation/p?.properties")
535+
static class ResourcePatternConfig {
536+
}
537+
538+
525539
@Configuration
526540
@PropertySources({
527541
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),

Diff for: spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @author Stephane Nicoll
4848
* @author Sam Brannen
49+
* @author Juergen Hoeller
4950
* @since 6.0
5051
* @see PropertySourceDescriptor
5152
*/
@@ -58,14 +59,14 @@ public class PropertySourceProcessor {
5859

5960
private final ConfigurableEnvironment environment;
6061

61-
private final ResourceLoader resourceLoader;
62+
private final ResourcePatternResolver resourcePatternResolver;
6263

6364
private final List<String> propertySourceNames = new ArrayList<>();
6465

6566

6667
public PropertySourceProcessor(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
6768
this.environment = environment;
68-
this.resourceLoader = resourceLoader;
69+
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
6970
}
7071

7172

@@ -87,8 +88,9 @@ public void processPropertySource(PropertySourceDescriptor descriptor) throws IO
8788
for (String location : locations) {
8889
try {
8990
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
90-
Resource resource = this.resourceLoader.getResource(resolvedLocation);
91-
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
91+
for (Resource resource : this.resourcePatternResolver.getResources(resolvedLocation)) {
92+
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
93+
}
9294
}
9395
catch (RuntimeException | IOException ex) {
9496
// Placeholders not resolvable (IllegalArgumentException) or resource not found when trying to open it

0 commit comments

Comments
 (0)