From d07a6ff0bb1c0b5e0f907ecb7508cb680ecd9e9f Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sat, 9 Mar 2019 20:49:47 +0100 Subject: [PATCH] Fix binding of bean with no bean definition See gh-16180 --- .../ConfigurationPropertiesBindingPostProcessor.java | 10 +++++++--- .../properties/ConfigurationPropertiesTests.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java index 40f9775c6f..161dc7a641 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java @@ -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) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index 3da426a42b..833e7664d6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -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);