Enable binding for scanned configuration properties
Closes gh-16822
This commit is contained in:
parent
2784fc1506
commit
89aefa6cfb
|
|
@ -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 {
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue