Skip to content

Commit 0041e24

Browse files
committed
Polishing
Issue: SPR-11637
1 parent d4b0ae9 commit 0041e24

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,15 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
305305
String name = propertySource.getString("name");
306306
String[] locations = propertySource.getStringArray("value");
307307
boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
308-
int locationCount = locations.length;
309-
if (locationCount == 0) {
308+
if (locations.length == 0) {
310309
throw new IllegalArgumentException("At least one @PropertySource(value) location is required");
311310
}
312311
for (String location : locations) {
313312
try {
314313
Resource resource = this.resourceLoader.getResource(
315314
this.environment.resolveRequiredPlaceholders(location));
316315
if (!StringUtils.hasText(name) || this.propertySources.containsKey(name)) {
317-
// We need to ensure unique names when the property source will
318-
// ultimately end up in a composite
316+
// We need to ensure unique names when the property source will ultimately end up in a composite
319317
ResourcePropertySource ps = new ResourcePropertySource(resource);
320318
this.propertySources.add((StringUtils.hasText(name) ? name : ps.getName()), ps);
321319
}

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -307,7 +307,7 @@ public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
307307
"Reason: Environment must implement ConfigurableEnvironment");
308308
}
309309
else {
310-
MutablePropertySources envPropertySources = ((ConfigurableEnvironment)this.environment).getPropertySources();
310+
MutablePropertySources envPropertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
311311
for (PropertySource<?> propertySource : parsedPropertySources) {
312312
envPropertySources.addLast(propertySource);
313313
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,6 @@ public void withResolvablePlaceholder() {
124124
System.clearProperty("path.to.properties");
125125
}
126126

127-
/**
128-
* Corner bug reported in SPR-9127.
129-
*/
130-
@Test
131-
public void withNameAndMultipleResourceLocations() {
132-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
133-
ctx.register(ConfigWithNameAndMultipleResourceLocations.class);
134-
ctx.refresh();
135-
assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
136-
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
137-
}
138-
139127
/**
140128
* SPR-10820
141129
*/
@@ -155,6 +143,24 @@ public void withEmptyResourceLocations() {
155143
ctx.refresh();
156144
}
157145

146+
@Test
147+
public void withNameAndMultipleResourceLocations() {
148+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
149+
assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
150+
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
151+
// p2 should 'win' as it was registered last
152+
assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
153+
}
154+
155+
@Test
156+
public void withMultipleResourceLocations() {
157+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
158+
assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
159+
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
160+
// p2 should 'win' as it was registered last
161+
assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
162+
}
163+
158164
@Test
159165
public void withPropertySources() {
160166
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithPropertySources.class);

spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,12 +29,11 @@
2929
*/
3030
public class CompositePropertySource extends PropertySource<Object> {
3131

32-
private Set<PropertySource<?>> propertySources = new LinkedHashSet<PropertySource<?>>();
32+
private final Set<PropertySource<?>> propertySources = new LinkedHashSet<PropertySource<?>>();
3333

3434

3535
/**
3636
* Create a new {@code CompositePropertySource}.
37-
*
3837
* @param name the name of the property source
3938
*/
4039
public CompositePropertySource(String name) {
@@ -60,6 +59,7 @@ public void addPropertySource(PropertySource<?> propertySource) {
6059
@Override
6160
public String toString() {
6261
return String.format("%s [name='%s', propertySources=%s]",
63-
this.getClass().getSimpleName(), this.name, this.propertySources);
62+
getClass().getSimpleName(), this.name, this.propertySources);
6463
}
64+
6565
}

0 commit comments

Comments
 (0)