|
|
|
@ -5,9 +5,13 @@ import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
import ${domain}.frame.utils.PropertiesUtil;
|
|
|
|
|
import ${domain}.frame.utils.StringUtil;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* LocalData - 本地数据存放类
|
|
|
|
@ -18,6 +22,8 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
*/
|
|
|
|
|
public class LocalData {
|
|
|
|
|
|
|
|
|
|
private static String[] applicationArgs = null;
|
|
|
|
|
|
|
|
|
|
private static ApplicationContext applicationContext = null;
|
|
|
|
|
|
|
|
|
|
private static Token system = null;
|
|
|
|
@ -60,8 +66,18 @@ public class LocalData {
|
|
|
|
|
return LocalData.applicationContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setApplicationContext(ApplicationContext applicationContext) {
|
|
|
|
|
LocalData.applicationContext = applicationContext;
|
|
|
|
|
public static void init(ApplicationContext context, String[] args) {
|
|
|
|
|
LocalData.applicationContext = context;
|
|
|
|
|
LocalData.applicationArgs = args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否初始化完成
|
|
|
|
|
*
|
|
|
|
|
* @return 是否
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isInited() {
|
|
|
|
|
return LocalData.applicationContext != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static <T> T getBean(Class<T> t) {
|
|
|
|
@ -79,9 +95,56 @@ public class LocalData {
|
|
|
|
|
return getBean(Environment.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getProperty(String key, String defaultValue) {
|
|
|
|
|
Environment environment = getEnvironment();
|
|
|
|
|
if (environment != null) {
|
|
|
|
|
return environment.getProperty(key, defaultValue);
|
|
|
|
|
} else {
|
|
|
|
|
String active = LocalData.getActive();
|
|
|
|
|
if (StringUtil.isEmpty(active)) {
|
|
|
|
|
return PropertiesUtil.getProp("application.properties", key, defaultValue);
|
|
|
|
|
} else {
|
|
|
|
|
return PropertiesUtil.getProp("application-" + active + ".properties", key, defaultValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getActive() {
|
|
|
|
|
String[] profiles = getEnvironment().getActiveProfiles();
|
|
|
|
|
return String.join(",", profiles);
|
|
|
|
|
// 当上下文初始化已完成后优先使用SpringBoot内存参数
|
|
|
|
|
Environment environment = getEnvironment();
|
|
|
|
|
if (environment != null) {
|
|
|
|
|
return environment.getProperty("spring.profiles.active", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String active = "";
|
|
|
|
|
// 1命令行环境 --spring.profiles.active=dev
|
|
|
|
|
if (applicationArgs != null) {
|
|
|
|
|
Pattern compile = Pattern.compile("--spring\\.profiles\\.active=(.*)");
|
|
|
|
|
for (String arg : applicationArgs) {
|
|
|
|
|
Matcher matcher = compile.matcher(arg);
|
|
|
|
|
if (matcher.find()) {
|
|
|
|
|
active = matcher.group(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (StringUtil.isNotEmpty(active)) {
|
|
|
|
|
return active;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2Java系统属性 -Dspring.profiles.active=dev
|
|
|
|
|
active = System.getProperty("spring.profiles.active");
|
|
|
|
|
if (StringUtil.isNotEmpty(active)) {
|
|
|
|
|
return active;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3操作系统环境变量 SPRING_PROFILES_ACTIVE=dev
|
|
|
|
|
active = System.getenv("SPRING_PROFILES_ACTIVE");
|
|
|
|
|
if (StringUtil.isNotEmpty(active)) {
|
|
|
|
|
return active;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4配置文件 spring.profiles.active=dev
|
|
|
|
|
return PropertiesUtil.getProp("application.properties", "spring.profiles.active", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getContext() {
|
|
|
|
|