记录项目配置,修改配置项的获取方式

- new class ` CommonEnvPostProcessor `
- remove ` @value `
- make JwtUtil's method static
This commit is contained in:
禾几海
2021-10-01 17:48:42 +08:00
parent 07e84ab875
commit 5a00e6c62f
17 changed files with 175 additions and 104 deletions

View File

@@ -0,0 +1,62 @@
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;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
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;
@Component
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 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("加载本地配置文件");
//获取环境变量
String homeEnv = EnvironmentUtil.getEnv("BLOG_HOME", EnvironmentUtil.getEnv("USERPROFILE"));
String configPath = (homeEnv + CONFIG_PATH).replaceAll("[\\|/]+", Matcher.quoteReplacement(File.separator));
try (InputStream input = new FileInputStream(configPath)) {
Properties properties = new Properties();
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);
} catch (Exception e) {
log.info("加载本地[" + configPath + "]的配置文件失败");
}
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
log.replayTo(CommonEnvPostProcessor.class);
}
@Override
public int getOrder() {
return 0;
}
}

View File

@@ -4,10 +4,10 @@ import cn.celess.common.constant.ResponseEnum;
import cn.celess.common.entity.Response;
import cn.celess.common.service.MailService;
import cn.celess.common.util.DateFormatUtil;
import cn.celess.common.util.EnvironmentUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
@@ -31,8 +31,6 @@ public class BlogExceptionHandler {
MailService mailService;
@Autowired
HttpServletRequest request;
@Value("${spring.profiles.active:dev}")
private String activeModel;
@ExceptionHandler(value = Exception.class)
@ResponseBody
@@ -65,7 +63,7 @@ public class BlogExceptionHandler {
}
// 发送错误信息到邮箱
if ("prod".equals(activeModel)) {
if ("prod".equals(EnvironmentUtil.getProperties("spring.profiles.active", "dev"))) {
logger.debug("有一个未捕获的bug已发送到邮箱");
sendMessage(e);
}

View File

@@ -0,0 +1,47 @@
package cn.celess.common.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.Properties;
@Slf4j
public class EnvironmentUtil {
private static final Properties properties = new Properties();
public static String getEnv(String name) {
String value = System.getenv(name);
if (StringUtils.isBlank(value)) {
log.error("没有找到环境变量:" + name);
}
return value;
}
public static String getEnv(String name, String defaultValue) {
String env = getEnv(name);
if (env == null) {
return defaultValue;
}
return env;
}
public static String getProperties(String key) {
String value = properties.getProperty(key);
if (StringUtils.isBlank(value)) {
log.error("没有找到配置项: {}", key);
}
return value;
}
public static String getProperties(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
public static void addProperties(Map<?, ?> map) {
properties.putAll(map);
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.env.EnvironmentPostProcessor=cn.celess.common.config.CommonEnvPostProcessor
org.springframework.context.ApplicationListener=cn.celess.common.config.CommonEnvPostProcessor