Skip to content

Commit

Permalink
fix:fix ApplicationContextAwareUtils NPE bug. (#1293)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman authored May 15, 2024
1 parent 8159888 commit f625e1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +33,8 @@
*/
public class ApplicationContextAwareUtils implements ApplicationContextAware {

private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationContextAwareUtils.class);

private static ApplicationContext applicationContext;

/**
Expand All @@ -50,7 +56,15 @@ 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);
}
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;
}

/**
Expand All @@ -60,6 +74,14 @@ 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);
}
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;
}
}

0 comments on commit f625e1d

Please sign in to comment.