fix:fix ApplicationContextAwareUtils NPE bug.

pull/1293/head
Haotian Zhang 1 year ago
parent 8159888db2
commit 09e0d14050

@ -12,3 +12,4 @@
- [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1275) - [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 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 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)

@ -17,6 +17,8 @@
package com.tencent.cloud.common.util; package com.tencent.cloud.common.util;
import com.tencent.polaris.api.utils.StringUtils;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
@ -50,7 +52,14 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware {
* @return property value * @return property value
*/ */
public static String getProperties(String key) { 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 class ApplicationContextAwareUtils implements ApplicationContextAware {
* @return property value * @return property value
*/ */
public static String getProperties(String key, String defaultValue) { 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;
} }
} }

Loading…
Cancel
Save