diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java index 6cf5cbdbaa5..1aec0338d1c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java @@ -253,7 +253,8 @@ public class PropertiesConfigurationFactory RelaxedDataBinder dataBinder = (this.targetName != null ? new RelaxedDataBinder(this.target, this.targetName) : new RelaxedDataBinder(this.target)); - if (this.validator != null) { + if (this.validator != null + && this.validator.supports(dataBinder.getTarget().getClass())) { dataBinder.setValidator(this.validator); } if (this.conversionService != null) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java index 5ec35125609..2a1eec9da3e 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.context.properties; import java.util.List; +import java.util.Map; import javax.annotation.PostConstruct; import javax.validation.constraints.NotNull; @@ -47,6 +48,7 @@ import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; import org.springframework.validation.annotation.Validated; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -330,6 +332,17 @@ public class ConfigurationPropertiesBindingPostProcessorTests { "Multiple PropertySourcesPlaceholderConfigurer beans registered"); } + @Test + public void propertiesWithMap() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, + "test.map.foo=bar"); + this.context.register(PropertiesWithMap.class); + this.context.refresh(); + assertThat(this.context.getBean(PropertiesWithMap.class).getMap()) + .containsEntry("foo", "bar"); + } + private void assertBindingFailure(int errorCount) { try { this.context.refresh(); @@ -608,6 +621,28 @@ public class ConfigurationPropertiesBindingPostProcessorTests { } + @Configuration + @EnableConfigurationProperties + @ConfigurationProperties(prefix = "test") + public static class PropertiesWithMap { + + @Bean + public Validator validator() { + return new LocalValidatorFactoryBean(); + } + + private Map map; + + public Map getMap() { + return this.map; + } + + public void setMap(Map map) { + this.map = map; + } + + } + @Configuration @EnableConfigurationProperties public static class ConfigurationPropertiesWithFactoryBean {