Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:fix ApplicationContextAwareUtils NPE bug. #1293

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading