diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java index be59be4635..ecf7e2732c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java @@ -148,8 +148,12 @@ public final class ConfigurationPropertiesBean { return getAll((ConfigurableApplicationContext) applicationContext); } Map propertiesBeans = new LinkedHashMap<>(); - applicationContext.getBeansWithAnnotation(ConfigurationProperties.class) - .forEach((beanName, bean) -> propertiesBeans.put(beanName, get(applicationContext, bean, beanName))); + applicationContext.getBeansWithAnnotation(ConfigurationProperties.class).forEach((beanName, bean) -> { + ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName); + if (propertiesBean != null) { + propertiesBeans.put(beanName, propertiesBean); + } + }); return propertiesBeans; } @@ -163,7 +167,9 @@ public final class ConfigurationPropertiesBean { try { Object bean = beanFactory.getBean(beanName); ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName); - propertiesBeans.put(beanName, propertiesBean); + if (propertiesBean != null) { + propertiesBeans.put(beanName, propertiesBean); + } } catch (Exception ex) { } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java index a11250b39d..3fad65e03a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java @@ -78,6 +78,15 @@ class ConfigurationPropertiesBeanTests { } } + @Test + void getAllDoesNotFindABeanDeclaredInAStaticBeanMethodOnAConfigurationAndConfigurationPropertiesAnnotatedClass() { + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( + StaticBeanMethodConfiguration.class)) { + Map all = ConfigurationPropertiesBean.getAll(context); + assertThat(all).containsOnlyKeys("configurationPropertiesBeanTests.StaticBeanMethodConfiguration"); + } + } + @Test void getAllWhenHasBadBeanDoesNotFail() { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( @@ -604,6 +613,18 @@ class ConfigurationPropertiesBeanTests { class ParameterizedConstructorInner { ParameterizedConstructorInner(Integer age) { + + } + + } + + @Configuration(proxyBeanMethods = false) + @ConfigurationProperties + static class StaticBeanMethodConfiguration { + + @Bean + static String stringBean() { + return "example"; } }