Rework @PropertySource early parsing logic
Rework the @PropertySource parsing logic recently changed in commit
7c608886 to deal with the same source appearing on a @Configuration
class and an @Import class.
Processing now occurs in a single sweep, with any previously added
sources being converted to a CompositePropertySource.
Issue: SPR-12115
This commit is contained in:
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -25,6 +27,7 @@ import java.util.Set;
|
||||
* share the same name, e.g. when multiple values are supplied to {@code @PropertySource}.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Phillip Webb
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public class CompositePropertySource extends PropertySource<Object> {
|
||||
@@ -56,6 +59,13 @@ public class CompositePropertySource extends PropertySource<Object> {
|
||||
this.propertySources.add(propertySource);
|
||||
}
|
||||
|
||||
public void addFirstPropertySource(PropertySource<?> propertySource) {
|
||||
List<PropertySource<?>> exisiting = new ArrayList<PropertySource<?>>(this.propertySources);
|
||||
this.propertySources.clear();
|
||||
this.propertySources.add(propertySource);
|
||||
this.propertySources.addAll(exisiting);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s [name='%s', propertySources=%s]",
|
||||
|
||||
@@ -115,7 +115,7 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
this(new DefaultResourceLoader().getResource(location));
|
||||
}
|
||||
|
||||
private ResourcePropertySource(String name, Map<String, Object> source) {
|
||||
protected ResourcePropertySource(String name, Map<String, Object> source) {
|
||||
super(name, source);
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
* empty, return the class name of the resource plus its identity hash code.
|
||||
* @see org.springframework.core.io.Resource#getDescription()
|
||||
*/
|
||||
private static String getNameForResource(Resource resource) {
|
||||
protected static String getNameForResource(Resource resource) {
|
||||
String name = resource.getDescription();
|
||||
if (!StringUtils.hasText(name)) {
|
||||
name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
|
||||
|
||||
46
spring-core/src/test/java/org/springframework/core/env/CompositePropertySourceTests.java
vendored
Normal file
46
spring-core/src/test/java/org/springframework/core/env/CompositePropertySourceTests.java
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link CompositePropertySource}.
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class CompositePropertySourceTests {
|
||||
|
||||
@Test
|
||||
public void addFirst() {
|
||||
PropertySource<?> p1 = new MapPropertySource("p1", Collections.emptyMap());
|
||||
PropertySource<?> p2 = new MapPropertySource("p2", Collections.emptyMap());
|
||||
PropertySource<?> p3 = new MapPropertySource("p3", Collections.emptyMap());
|
||||
CompositePropertySource composite = new CompositePropertySource("c");
|
||||
composite.addPropertySource(p2);
|
||||
composite.addPropertySource(p3);
|
||||
composite.addPropertySource(p1);
|
||||
composite.addFirstPropertySource(p1);
|
||||
assertThat(composite.toString(), containsString("MapPropertySource [name='p1'], "
|
||||
+ "MapPropertySource [name='p2'], MapPropertySource [name='p3']"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user