Skip to content

Commit 80dd32e

Browse files
committed
Disallow empty @propertysource(value = {})
Previously, a user could specify an empty array of resource locations to the @propertysource annotation, which amounts to a meaningless no-op. ConfigurationClassParser now throws IllegalArgumentException upon encountering any such misconfiguration.
1 parent 41ade68 commit 80dd32e

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ protected AnnotationMetadata doProcessConfigurationClass(
181181
String name = propertySource.getString("name");
182182
String[] locations = propertySource.getStringArray("value");
183183
int nLocations = locations.length;
184+
if (nLocations == 0) {
185+
throw new IllegalArgumentException("At least one @PropertySource(value) location is required");
186+
}
184187
for (int i = 0; i < nLocations; i++) {
185188
locations[0] = this.environment.resolveRequiredPlaceholders(locations[0]);
186189
}

org.springframework.context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,11 @@ public void withNameAndMultipleResourceLocations() {
130130
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
131131
}
132132

133-
134-
@Configuration
135-
@PropertySource(
136-
name = "psName",
137-
value = {
138-
"classpath:org/springframework/context/annotation/p1.properties",
139-
"classpath:org/springframework/context/annotation/p2.properties"
140-
})
141-
static class ConfigWithNameAndMultipleResourceValues {
133+
@Test(expected=IllegalArgumentException.class)
134+
public void withEmptyResourceLocations() {
135+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
136+
ctx.register(ConfigWithEmptyResourceLocations.class);
137+
ctx.refresh();
142138
}
143139

144140

@@ -212,4 +208,10 @@ static class P2Config {
212208
})
213209
static class ConfigWithNameAndMultipleResourceLocations {
214210
}
211+
212+
213+
@Configuration
214+
@PropertySource(value = {})
215+
static class ConfigWithEmptyResourceLocations {
216+
}
215217
}

0 commit comments

Comments
 (0)