From c4831aabf0659344348f0045a32133499b7d6f8e Mon Sep 17 00:00:00 2001 From: Haotian Zhang Date: Wed, 15 May 2024 16:36:58 +0800 Subject: [PATCH] fix:add warn log to ApplicationContextAwareUtils. (#1297) --- CHANGELOG.md | 3 ++- .../util/ApplicationContextAwareUtils.java | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 051689b2..8b8bcce3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,4 +10,5 @@ - [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1266) - [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1272) - [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1274) -- [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1279) \ No newline at end of file +- [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1279) +- [fix:fix ApplicationContextAwareUtils NPE bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1297) 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 98a6a434..60421b02 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,10 @@ 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; import org.springframework.context.ApplicationContextAware; @@ -29,6 +33,8 @@ import org.springframework.lang.NonNull; */ public class ApplicationContextAwareUtils implements ApplicationContextAware { + private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationContextAwareUtils.class); + private static ApplicationContext applicationContext; /** @@ -50,7 +56,15 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware { * @return property value */ public static String getProperties(String key) { - return applicationContext.getEnvironment().getProperty(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); + } + return property; } /** @@ -60,6 +74,14 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware { * @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); + } + 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); + } + return property; } }