Fix ordering of keys in PropertySourcesPropertyValues

Since @ConfigurationProperties binding uses a single instance of
PropertySourcesPropertyValues per bean, there doesn't seem to be
any issue with using a normal LinkedHashMap. Then the order
passed in as PropertySources will be preserved.

Fixes gh-2487
pull/2150/merge
Dave Syer 10 years ago
parent 0ef3de4d82
commit 12724bf332

@ -19,8 +19,8 @@ package org.springframework.boot.bind;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
@ -42,7 +42,7 @@ import org.springframework.validation.DataBinder;
*/
public class PropertySourcesPropertyValues implements PropertyValues {
private final Map<String, PropertyValue> propertyValues = new ConcurrentHashMap<String, PropertyValue>();
private final Map<String, PropertyValue> propertyValues = new LinkedHashMap<String, PropertyValue>();
private final PropertySources propertySources;

@ -16,11 +16,14 @@
package org.springframework.boot.bind;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.PropertyValue;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
@ -61,6 +64,26 @@ public class PropertySourcesPropertyValuesTests {
assertEquals(1, propertyValues.getPropertyValues().length);
}
@Test
public void testOrderPreserved() {
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
map.put("five", 5);
this.propertySources.addFirst(new MapPropertySource("ordered", map));
PropertySourcesPropertyValues propertyValues = new PropertySourcesPropertyValues(
this.propertySources);
PropertyValue[] values = propertyValues.getPropertyValues();
assertEquals(6, values.length);
Collection<String> names = new ArrayList<String>();
for (PropertyValue value : values) {
names.add(value.getName());
}
assertEquals("[one, two, three, four, five, name]", names.toString());
}
@Test
public void testNonEnumeratedValue() {
PropertySourcesPropertyValues propertyValues = new PropertySourcesPropertyValues(

Loading…
Cancel
Save