Fix binding of bean with no bean definition

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

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

@ -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, private AnnotationConfigApplicationContext load(Class<?> configuration,
String... inlinedProperties) { String... inlinedProperties) {
return load(new Class<?>[] { configuration }, inlinedProperties); return load(new Class<?>[] { configuration }, inlinedProperties);

Loading…
Cancel
Save