mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Add support for binding to Properties in @ConfigurationProperties
Fixes gh-4250
This commit is contained in:
parent
b76414002a
commit
90ddd857f4
2
spring-boot-dependencies/pom.xml
Executable file → Normal file
2
spring-boot-dependencies/pom.xml
Executable file → Normal file
@ -2185,4 +2185,4 @@
|
||||
<id>integration-test</id>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
</project>
|
@ -25,6 +25,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
@ -311,6 +312,10 @@ public class RelaxedDataBinder extends DataBinder {
|
||||
if (descriptor == null || descriptor.getMapValueTypeDescriptor() == null) {
|
||||
return false;
|
||||
}
|
||||
if (Properties.class.isAssignableFrom(descriptor.getObjectType())) {
|
||||
// Properties is declared as Map<Object,Object> but we know it's really Map<String,String>
|
||||
return true;
|
||||
}
|
||||
Class<?> valueType = descriptor.getMapValueTypeDescriptor().getObjectType();
|
||||
return (valueType != null && CharSequence.class.isAssignableFrom(valueType));
|
||||
}
|
||||
|
@ -16,6 +16,14 @@
|
||||
|
||||
package org.springframework.boot.bind;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@ -40,7 +48,6 @@ import javax.validation.constraints.NotNull;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.NotWritablePropertyException;
|
||||
import org.springframework.context.support.StaticMessageSource;
|
||||
@ -53,14 +60,6 @@ import org.springframework.validation.DataBinder;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link RelaxedDataBinder}.
|
||||
*
|
||||
@ -371,6 +370,14 @@ public class RelaxedDataBinderTests {
|
||||
assertEquals("123", target.getNested().get("value.foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindNestedProperties() throws Exception {
|
||||
TargetWithNestedProperties target = new TargetWithNestedProperties();
|
||||
bind(target, "nested.foo: bar\n" + "nested.value.foo: 123");
|
||||
assertEquals("bar", target.getNested().get("foo"));
|
||||
assertEquals("123", target.getNested().get("value.foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindNestedMapOfEnum() throws Exception {
|
||||
this.conversionService = new DefaultConversionService();
|
||||
@ -780,6 +787,20 @@ public class RelaxedDataBinderTests {
|
||||
|
||||
}
|
||||
|
||||
public static class TargetWithNestedProperties {
|
||||
|
||||
private Properties nested;
|
||||
|
||||
public Properties getNested() {
|
||||
return this.nested;
|
||||
}
|
||||
|
||||
public void setNested(Properties nested) {
|
||||
this.nested = nested;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class TargetWithNestedMapOfString {
|
||||
|
||||
private Map<String, String> nested;
|
||||
|
Loading…
Reference in New Issue
Block a user