Skip to content

fix:add warn log to ApplicationContextAwareUtils. #1295

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

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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
- [feat:upgrade jackson version.](https://github.com/Tencent/spring-cloud-tencent/pull/1257)
- [fix:fix wrong report when using Zuul with instance not found exception.](https://github.com/Tencent/spring-cloud-tencent/pull/1283)
- [feat: merge lane router and lossless features from 2023](https://github.com/Tencent/spring-cloud-tencent/pull/1288)
- [fix: fix npe when add circuitbreak module without feign.hystrix.enable=true](https://github.com/Tencent/spring-cloud-tencent/pull/1292)
- [fix: fix npe when add circuitbreak module without feign.hystrix.enable=true](https://github.com/Tencent/spring-cloud-tencent/pull/1292)
- [fix:fix ApplicationContextAwareUtils NPE bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1295)
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