From a1fe05f40b02cc79dc3777ff6f90a04ac4de6eac Mon Sep 17 00:00:00 2001 From: qxo <49526356@qq.com> Date: Fri, 4 Mar 2022 22:55:31 +0800 Subject: [PATCH] Fix NPE in configprops endpoint This works around spring-projects/spring-framework#28298. The bug means that when a @Configuration class is annotated with @ConfigurationProperties any bean defined by a static @Bean method is considered to be annotated with @ConfigurationProperties. See gh-30068 --- .../ConfigurationPropertiesReportEndpoint.java | 2 +- .../properties/ConfigurationPropertiesBean.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java index c79c58a0603..23b8d8d12e0 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java @@ -121,7 +121,7 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext @ReadOperation public ApplicationConfigurationProperties configurationProperties() { - return extract(this.context, (bean) -> true); + return extract(this.context, (bean) -> bean != null); } @ReadOperation 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 b5628a5f687..b95fee19189 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 @@ -144,7 +144,13 @@ public final class ConfigurationPropertiesBean { } Map propertiesBeans = new LinkedHashMap<>(); applicationContext.getBeansWithAnnotation(ConfigurationProperties.class) - .forEach((beanName, bean) -> propertiesBeans.put(beanName, get(applicationContext, bean, beanName))); + .forEach((beanName, bean) -> { + ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName); + if (propertiesBean == null) { //ignore for null + return; + } + propertiesBeans.put(beanName,propertiesBean); + }); return propertiesBeans; } @@ -158,6 +164,9 @@ public final class ConfigurationPropertiesBean { try { Object bean = beanFactory.getBean(beanName); ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName); + if (propertiesBean == null) { //ignore for null + continue; + } propertiesBeans.put(beanName, propertiesBean); } catch (Exception ex) {