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);