EnvironmentUtil工具类
This commit is contained in:
@@ -3,7 +3,6 @@ package cn.celess.common.config;
|
||||
import cn.celess.common.util.EnvironmentUtil;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||
import org.springframework.boot.logging.DeferredLog;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -15,7 +14,6 @@ import org.springframework.stereotype.Component;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
@@ -23,18 +21,15 @@ import java.util.regex.Matcher;
|
||||
public class CommonEnvPostProcessor implements EnvironmentPostProcessor, ApplicationListener<ApplicationEvent>, Ordered {
|
||||
public static final DeferredLog log = new DeferredLog();
|
||||
|
||||
private static final String CONFIG_PATH = "/config/blog.properties";
|
||||
private static final String CONFIG_PATH = "/HBlog/config/blog.properties";
|
||||
private static final String SOURCE_NAME = "localize";
|
||||
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
|
||||
configurableEnvironment.getPropertySources().forEach(propertySource -> {
|
||||
if (propertySource.getName().startsWith("applicationConfig") && propertySource instanceof OriginTrackedMapPropertySource) {
|
||||
EnvironmentUtil.addProperties((Map<?, ?>) propertySource.getSource());
|
||||
}
|
||||
});
|
||||
|
||||
log.info("加载本地配置文件");
|
||||
EnvironmentUtil.setEnvironment(configurableEnvironment);
|
||||
|
||||
log.info("加载本地配置文件--");
|
||||
//获取环境变量
|
||||
String homeEnv = EnvironmentUtil.getEnv("BLOG_HOME", EnvironmentUtil.getEnv("USERPROFILE"));
|
||||
String configPath = (homeEnv + CONFIG_PATH).replaceAll("[\\|/]+", Matcher.quoteReplacement(File.separator));
|
||||
@@ -43,10 +38,9 @@ public class CommonEnvPostProcessor implements EnvironmentPostProcessor, Applica
|
||||
properties.load(input);
|
||||
PropertiesPropertySource propertySource = new PropertiesPropertySource(SOURCE_NAME, properties);
|
||||
configurableEnvironment.getPropertySources().addLast(propertySource);
|
||||
log.info("Load the configuration file under the environment variable,end.");
|
||||
EnvironmentUtil.addProperties(properties);
|
||||
log.info("成功加载本地配置文件:)");
|
||||
} catch (Exception e) {
|
||||
log.info("加载本地[" + configPath + "]的配置文件失败");
|
||||
log.info("加载本地[" + configPath + "]的配置文件失败:(");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,12 @@ package cn.celess.common.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@Slf4j
|
||||
public class EnvironmentUtil {
|
||||
|
||||
private static final Properties properties = new Properties();
|
||||
|
||||
private static Environment environment;
|
||||
|
||||
public static String getEnv(String name) {
|
||||
String value = System.getenv(name);
|
||||
@@ -29,7 +26,7 @@ public class EnvironmentUtil {
|
||||
}
|
||||
|
||||
public static String getProperties(String key) {
|
||||
String value = properties.getProperty(key);
|
||||
String value = environment.getProperty(key);
|
||||
if (StringUtils.isBlank(value)) {
|
||||
log.error("没有找到配置项: {}", key);
|
||||
}
|
||||
@@ -37,11 +34,10 @@ public class EnvironmentUtil {
|
||||
}
|
||||
|
||||
public static String getProperties(String key, String defaultValue) {
|
||||
return properties.getProperty(key, defaultValue);
|
||||
return environment.getProperty(key, defaultValue);
|
||||
}
|
||||
|
||||
|
||||
public static void addProperties(Map<?, ?> map) {
|
||||
properties.putAll(map);
|
||||
public static void setEnvironment(Environment environment) {
|
||||
EnvironmentUtil.environment = environment;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user