From 0f2190893683fb7106ee54df777e6374667f906a Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 13 May 2024 15:31:51 +0800 Subject: [PATCH] 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 5f44d8d449..951c50e1fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,4 @@ --- - [feat:upgrade jackson version.](https://github.com/Tencent/spring-cloud-tencent/pull/1259) +- [fix:fix ApplicationContextAwareUtils NPE bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1294) 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; } }