diff --git a/src/main/resources/modules/SpringBoot/java/Application.ftl b/src/main/resources/modules/SpringBoot/java/Application.ftl index 86cc2edf..cd042b4b 100644 --- a/src/main/resources/modules/SpringBoot/java/Application.ftl +++ b/src/main/resources/modules/SpringBoot/java/Application.ftl @@ -24,6 +24,6 @@ public class Application extends SpringBootServletInitializer { } public static void main(String[] args) { - LocalData.setApplicationContext(SpringApplication.run(Application.class, args)); + LocalData.init(SpringApplication.run(Application.class, args), args); } } \ No newline at end of file diff --git a/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java b/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java index 2ed3bcf9..c1981aee 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java +++ b/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java @@ -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 getBean(Class t) { @@ -79,12 +95,59 @@ 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() { return getRequest().getContextPath(); } -} +} \ No newline at end of file diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/PropertiesUtil.java b/src/main/resources/modules/SpringBoot/java/frame/utils/PropertiesUtil.java new file mode 100644 index 00000000..ae2f8f65 --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/PropertiesUtil.java @@ -0,0 +1,54 @@ +package ${domain}.frame.utils; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * 配置属性获取工具 + * + * @author wangbing + * @version 0.0.1 + * @since 2020-11-10 + */ +public class PropertiesUtil { + + /** + * 属性集合缓存 + */ + private static Map propertiesMap = new HashMap<>(); + + /** + * @param name 属性文件名称 + * @param key 属性关键字 + * @param defaultValue 当取不到值时的默认值 + * @return + */ + public static String getProp(String name, String key, String defaultValue) { + Properties properties = propertiesMap.get(name); + if (properties == null) { + try { + properties = new Properties(); + properties.load(ResourceUtil.getInput(name)); + propertiesMap.put(name, properties); + } catch (IOException ignored) { + + } + } + if (properties == null) { + return ""; + } + return properties.getProperty(key, defaultValue); + } + + public static String getProp(String name, String key) { + return getProp(name, key); + } + + public static int getPropInt(String name, String key, int defaultValue) { + String prop = getProp(name, key, String.valueOf(defaultValue)); + // 这边对值不做容错处理,预防因为容错出现难以查找的BUG + return Integer.parseInt(prop); + } +} \ No newline at end of file diff --git a/src/main/resources/modules/SpringBoot/test/TestConfig.ftl b/src/main/resources/modules/SpringBoot/test/TestConfig.ftl index 7a0aa8be..1c9b1ee1 100644 --- a/src/main/resources/modules/SpringBoot/test/TestConfig.ftl +++ b/src/main/resources/modules/SpringBoot/test/TestConfig.ftl @@ -17,7 +17,7 @@ public class TestConfig { @PostConstruct public void initLocalData() { - LocalData.setApplicationContext(applicationContext); + LocalData.init(applicationContext,new String[]{}); } @Bean