EnvironmentUtil工具类
This commit is contained in:
@@ -3,7 +3,6 @@ package cn.celess.common.config;
|
|||||||
import cn.celess.common.util.EnvironmentUtil;
|
import cn.celess.common.util.EnvironmentUtil;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
|
||||||
import org.springframework.boot.logging.DeferredLog;
|
import org.springframework.boot.logging.DeferredLog;
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
@@ -15,7 +14,6 @@ import org.springframework.stereotype.Component;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
@@ -23,18 +21,15 @@ import java.util.regex.Matcher;
|
|||||||
public class CommonEnvPostProcessor implements EnvironmentPostProcessor, ApplicationListener<ApplicationEvent>, Ordered {
|
public class CommonEnvPostProcessor implements EnvironmentPostProcessor, ApplicationListener<ApplicationEvent>, Ordered {
|
||||||
public static final DeferredLog log = new DeferredLog();
|
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";
|
private static final String SOURCE_NAME = "localize";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
|
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 homeEnv = EnvironmentUtil.getEnv("BLOG_HOME", EnvironmentUtil.getEnv("USERPROFILE"));
|
||||||
String configPath = (homeEnv + CONFIG_PATH).replaceAll("[\\|/]+", Matcher.quoteReplacement(File.separator));
|
String configPath = (homeEnv + CONFIG_PATH).replaceAll("[\\|/]+", Matcher.quoteReplacement(File.separator));
|
||||||
@@ -43,10 +38,9 @@ public class CommonEnvPostProcessor implements EnvironmentPostProcessor, Applica
|
|||||||
properties.load(input);
|
properties.load(input);
|
||||||
PropertiesPropertySource propertySource = new PropertiesPropertySource(SOURCE_NAME, properties);
|
PropertiesPropertySource propertySource = new PropertiesPropertySource(SOURCE_NAME, properties);
|
||||||
configurableEnvironment.getPropertySources().addLast(propertySource);
|
configurableEnvironment.getPropertySources().addLast(propertySource);
|
||||||
log.info("Load the configuration file under the environment variable,end.");
|
log.info("成功加载本地配置文件:)");
|
||||||
EnvironmentUtil.addProperties(properties);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("加载本地[" + configPath + "]的配置文件失败");
|
log.info("加载本地[" + configPath + "]的配置文件失败:(");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,15 +2,12 @@ package cn.celess.common.util;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class EnvironmentUtil {
|
public class EnvironmentUtil {
|
||||||
|
|
||||||
private static final Properties properties = new Properties();
|
private static Environment environment;
|
||||||
|
|
||||||
|
|
||||||
public static String getEnv(String name) {
|
public static String getEnv(String name) {
|
||||||
String value = System.getenv(name);
|
String value = System.getenv(name);
|
||||||
@@ -29,7 +26,7 @@ public class EnvironmentUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getProperties(String key) {
|
public static String getProperties(String key) {
|
||||||
String value = properties.getProperty(key);
|
String value = environment.getProperty(key);
|
||||||
if (StringUtils.isBlank(value)) {
|
if (StringUtils.isBlank(value)) {
|
||||||
log.error("没有找到配置项: {}", key);
|
log.error("没有找到配置项: {}", key);
|
||||||
}
|
}
|
||||||
@@ -37,11 +34,10 @@ public class EnvironmentUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getProperties(String key, String defaultValue) {
|
public static String getProperties(String key, String defaultValue) {
|
||||||
return properties.getProperty(key, defaultValue);
|
return environment.getProperty(key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setEnvironment(Environment environment) {
|
||||||
public static void addProperties(Map<?, ?> map) {
|
EnvironmentUtil.environment = environment;
|
||||||
properties.putAll(map);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ spring.datasource.password=
|
|||||||
spring.datasource.platform=h2
|
spring.datasource.platform=h2
|
||||||
spring.datasource.sql-script-encoding=utf-8
|
spring.datasource.sql-script-encoding=utf-8
|
||||||
spring.datasource.initialization-mode=ALWAYS
|
spring.datasource.initialization-mode=ALWAYS
|
||||||
spring.datasource.schema=classpath*:sql/schema_h2.sql
|
spring.datasource.schema=classpath:sql/schema_h2.sql
|
||||||
spring.datasource.data=classpath*:sql/data.sql
|
spring.datasource.data=classpath:sql/data.sql
|
||||||
################## mybatis ##################
|
################## mybatis ##################
|
||||||
mybatis.mapper-locations=classpath*:mapper/*.xml
|
mybatis.mapper-locations=classpath*:mapper/*.xml
|
||||||
mybatis.type-aliases-package=cn.celess.common.entity
|
mybatis.type-aliases-package=cn.celess.common.entity
|
||||||
|
|||||||
Reference in New Issue
Block a user