From 12724bf33211d68899d710fda376bd5636731e4c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 12 Feb 2015 10:09:19 +0000 Subject: [PATCH] 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 --- .../bind/PropertySourcesPropertyValues.java | 4 ++-- .../PropertySourcesPropertyValuesTests.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java index 07307ecc4e..45ae709097 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java @@ -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 propertyValues = new ConcurrentHashMap(); + private final Map propertyValues = new LinkedHashMap(); private final PropertySources propertySources; diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java index a4b19af6c2..03394de45c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java @@ -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 map = new LinkedHashMap(); + 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 names = new ArrayList(); + 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(