Fix binding of bean with no bean definition

See gh-16180
pull/16193/head
Stephane Nicoll 6 years ago
parent f5eb853f26
commit d07a6ff0bb

@ -102,9 +102,13 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
}
private boolean hasBeenBound(String beanName) {
BeanDefinition beanDefinition = ((BeanDefinitionRegistry) this.applicationContext
.getAutowireCapableBeanFactory()).getBeanDefinition(beanName);
return beanDefinition instanceof ConfigurationPropertiesBeanDefinition;
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) this.applicationContext
.getAutowireCapableBeanFactory();
if (registry.containsBeanDefinition(beanName)) {
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
return beanDefinition instanceof ConfigurationPropertiesBeanDefinition;
}
return false;
}
private void bind(Object bean, String beanName, ConfigurationProperties annotation) {

@ -846,6 +846,17 @@ public class ConfigurationPropertiesTests {
});
}
@Test
public void loadWhenBindingOnBeanWithoutBeanDefinitionShouldBind() {
load(BasicConfiguration.class, "name=test");
BasicProperties bean = this.context.getBean(BasicProperties.class);
assertThat(bean.name).isEqualTo("test");
bean.name = "override";
this.context.getBean(ConfigurationPropertiesBindingPostProcessor.class)
.postProcessBeforeInitialization(bean, "does-not-exist");
assertThat(bean.name).isEqualTo("test");
}
private AnnotationConfigApplicationContext load(Class<?> configuration,
String... inlinedProperties) {
return load(new Class<?>[] { configuration }, inlinedProperties);

Loading…
Cancel
Save