From 09e0d14050b3bde2042382a1159a66cded4a4ad7 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 13 May 2024 15:31:51 +0800 Subject: [PATCH 1/2] fix:fix ApplicationContextAwareUtils NPE bug. --- CHANGELOG.md | 1 + .../util/ApplicationContextAwareUtils.java | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b45c5d152f..267a2c8325 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ - [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1275) - [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1276) - [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1281) +- [fix:fix ApplicationContextAwareUtils NPE bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1293) diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java index 98a6a4341a..be90be2f4b 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java @@ -17,6 +17,8 @@ package com.tencent.cloud.common.util; +import com.tencent.polaris.api.utils.StringUtils; + import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -50,7 +52,14 @@ public void setApplicationContext(@NonNull ApplicationContext applicationContext * @return property value */ public static String getProperties(String key) { - return applicationContext.getEnvironment().getProperty(key); + if (applicationContext != null) { + return applicationContext.getEnvironment().getProperty(key); + } + String property = System.getenv(key); + if (StringUtils.isBlank(property)) { + property = System.getProperty(key); + } + return property; } /** @@ -60,6 +69,13 @@ public static String getProperties(String key) { * @return property value */ public static String getProperties(String key, String defaultValue) { - return applicationContext.getEnvironment().getProperty(key, defaultValue); + if (applicationContext != null) { + return applicationContext.getEnvironment().getProperty(key, defaultValue); + } + String property = System.getenv(key); + if (StringUtils.isBlank(property)) { + property = System.getProperty(key, defaultValue); + } + return property; } } From fd732864aafec01c7079a76b1cc6fffe050e049b Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 15 May 2024 11:54:02 +0800 Subject: [PATCH 2/2] fix:fix ApplicationContextAwareUtils NPE bug. --- .../cloud/common/util/ApplicationContextAwareUtils.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java index be90be2f4b..60421b02c0 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ApplicationContextAwareUtils.java @@ -18,6 +18,8 @@ package com.tencent.cloud.common.util; import com.tencent.polaris.api.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; @@ -31,6 +33,8 @@ */ public class ApplicationContextAwareUtils implements ApplicationContextAware { + private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationContextAwareUtils.class); + private static ApplicationContext applicationContext; /** @@ -55,6 +59,7 @@ public static String getProperties(String key) { if (applicationContext != null) { return applicationContext.getEnvironment().getProperty(key); } + LOGGER.warn("applicationContext is null, try to get property from System.getenv or System.getProperty"); String property = System.getenv(key); if (StringUtils.isBlank(property)) { property = System.getProperty(key); @@ -72,6 +77,7 @@ public static String getProperties(String key, String defaultValue) { if (applicationContext != null) { return applicationContext.getEnvironment().getProperty(key, defaultValue); } + LOGGER.warn("applicationContext is null, try to get property from System.getenv or System.getProperty"); String property = System.getenv(key); if (StringUtils.isBlank(property)) { property = System.getProperty(key, defaultValue);