fix:fix ApplicationContextAwareUtils NPE bug.

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

@ -18,6 +18,8 @@
package com.tencent.cloud.common.util; package com.tencent.cloud.common.util;
import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -31,6 +33,8 @@ import org.springframework.lang.NonNull;
*/ */
public class ApplicationContextAwareUtils implements ApplicationContextAware { public class ApplicationContextAwareUtils implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationContextAwareUtils.class);
private static ApplicationContext applicationContext; private static ApplicationContext applicationContext;
/** /**
@ -55,6 +59,7 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware {
if (applicationContext != null) { if (applicationContext != null) {
return applicationContext.getEnvironment().getProperty(key); 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); String property = System.getenv(key);
if (StringUtils.isBlank(property)) { if (StringUtils.isBlank(property)) {
property = System.getProperty(key); property = System.getProperty(key);
@ -72,6 +77,7 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware {
if (applicationContext != null) { if (applicationContext != null) {
return applicationContext.getEnvironment().getProperty(key, defaultValue); 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); String property = System.getenv(key);
if (StringUtils.isBlank(property)) { if (StringUtils.isBlank(property)) {
property = System.getProperty(key, defaultValue); property = System.getProperty(key, defaultValue);

Loading…
Cancel
Save