|
|
|
@ -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;
|
|
|
|
@ -29,6 +33,8 @@ import org.springframework.lang.NonNull;
|
|
|
|
|
*/
|
|
|
|
|
public class ApplicationContextAwareUtils implements ApplicationContextAware {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationContextAwareUtils.class);
|
|
|
|
|
|
|
|
|
|
private static ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -50,8 +56,16 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware {
|
|
|
|
|
* @return property value
|
|
|
|
|
*/
|
|
|
|
|
public static String getProperties(String 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get application property. If null, return default.
|
|
|
|
@ -60,6 +74,14 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware {
|
|
|
|
|
* @return property value
|
|
|
|
|
*/
|
|
|
|
|
public static String getProperties(String key, String 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|