diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java index e900d5d422..6a002b71e1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java @@ -37,7 +37,7 @@ import org.springframework.core.annotation.AliasFor; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented -@Import(ConfigurationPropertiesScanRegistrar.class) +@Import({ ConfigurationPropertiesScanRegistrar.class, ConfigurationPropertiesBindingPostProcessorRegistrar.class }) public @interface ConfigurationPropertiesScan { /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java index 8875e572b1..099ba88b78 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java @@ -92,7 +92,9 @@ class ConfigurationPropertiesScanRegistrarTests { BeanDefinition bSecondDefinition = beanFactory.getBeanDefinition( "b.second-org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration$BSecondProperties"); assertThat(aDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); - assertThat(bFirstDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); + // Constructor injection + assertThat(bFirstDefinition).isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); + // Post-processing injection assertThat(bSecondDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanTests.java index 182ae22de5..5609874e90 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanTests.java @@ -27,12 +27,14 @@ import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.boot.context.properties.scan.valid.a.AScanConfiguration; import org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration.BFirstProperties; import org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration.BProperties; +import org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration.BSecondProperties; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.AssignableTypeFilter; +import org.springframework.test.context.support.TestPropertySourceUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @@ -44,6 +46,7 @@ import static org.mockito.Mockito.mock; * * @author Madhura Bhave * @author Johnny Lim + * @author Stephane Nicoll */ class ConfigurationPropertiesScanTests { @@ -115,8 +118,16 @@ class ConfigurationPropertiesScanTests { "b.first-org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration$BFirstProperties"); } - private void load(Class... classes) { - this.context.register(classes); + @Test + void scanShouldBindConfigurationProperties() { + load(TestAnotherPackageConfiguration.class, "b.first.name=constructor", "b.second.number=42"); + assertThat(this.context.getBean(BFirstProperties.class).getName()).isEqualTo("constructor"); + assertThat(this.context.getBean(BSecondProperties.class).getNumber()).isEqualTo(42); + } + + private void load(Class configuration, String... inlinedProperties) { + this.context.register(configuration); + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, inlinedProperties); this.context.refresh(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java index 6e67184817..7ffedd0539 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java @@ -30,11 +30,31 @@ public class BScanConfiguration { @ConfigurationProperties(prefix = "b.first") public static class BFirstProperties implements BProperties { + private final String name; + + public BFirstProperties(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + } @ConfigurationProperties(prefix = "b.second") public static class BSecondProperties implements BProperties { + private int number; + + public int getNumber() { + return this.number; + } + + public void setNumber(int number) { + this.number = number; + } + } }