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 b5628a5f68..a3c86d8b30 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -143,8 +143,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; } @@ -158,7 +162,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 63a006cab0..407f794c3f 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( @@ -546,4 +555,15 @@ class ConfigurationPropertiesBeanTests { } + @Configuration(proxyBeanMethods = false) + @ConfigurationProperties + static class StaticBeanMethodConfiguration { + + @Bean + static String stringBean() { + return "example"; + } + + } + }