Pre Merge pull request !5 from tangjl/master

This commit is contained in:
tangjl 2023-07-08 06:22:16 +00:00 committed by Gitee
commit 06f05cad76
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -65,24 +65,26 @@ public class BladeApplication {
List<String> presetProfiles = new ArrayList<>(Arrays.asList(AppConstant.DEV_CODE, AppConstant.TEST_CODE, AppConstant.PROD_CODE)); List<String> presetProfiles = new ArrayList<>(Arrays.asList(AppConstant.DEV_CODE, AppConstant.TEST_CODE, AppConstant.PROD_CODE));
// 交集 // 交集
presetProfiles.retainAll(profiles); presetProfiles.retainAll(profiles);
// 当前使用
List<String> activeProfileList = new ArrayList<>(profiles);
Function<Object[], String> joinFun = StringUtils::arrayToCommaDelimitedString; Function<Object[], String> joinFun = StringUtils::arrayToCommaDelimitedString;
SpringApplicationBuilder builder = new SpringApplicationBuilder(source);
String profile; String profile;
if (activeProfileList.isEmpty()) { if(presetProfiles.isEmpty()){
// 默认dev开发 // 未指定环境默认dev
profile = AppConstant.DEV_CODE; profile = AppConstant.DEV_CODE;
activeProfileList.add(profile); profiles.add(profile);
builder.profiles(profile); } else if (presetProfiles.size() == 1) {
} else if (activeProfileList.size() == 1) { // 已指定唯一环境
profile = activeProfileList.get(0); profile = presetProfiles.get(0);
} else { } else {
// 同时存在devtestprod环境时 // 同时存在devtestprod环境时
throw new RuntimeException("同时存在环境变量:[" + StringUtils.arrayToCommaDelimitedString(activeProfiles) + "]"); throw new RuntimeException("同时存在环境变量:[" + joinFun.apply(presetProfiles.toArray()) + "]");
} }
// 设置激活的Profile
SpringApplicationBuilder builder = new SpringApplicationBuilder(source);
builder.profiles(presetProfiles.toArray(new String[presetProfiles.size()]));
String startJarPath = BladeApplication.class.getResource("/").getPath().split("!")[0]; String startJarPath = BladeApplication.class.getResource("/").getPath().split("!")[0];
String activePros = joinFun.apply(activeProfileList.toArray()); String activePros = joinFun.apply(profiles.toArray());
System.out.println(String.format("----启动中,读取到的环境变量:[%s]jar地址:[%s]----", activePros, startJarPath)); System.out.println(String.format("----启动中,读取到的环境变量:[%s]jar地址:[%s]----", activePros, startJarPath));
Properties props = System.getProperties(); Properties props = System.getProperties();
props.setProperty("spring.application.name", appName); props.setProperty("spring.application.name", appName);
@ -102,7 +104,8 @@ public class BladeApplication {
// 加载自定义组件 // 加载自定义组件
List<LauncherService> launcherList = new ArrayList<>(); List<LauncherService> launcherList = new ArrayList<>();
ServiceLoader.load(LauncherService.class).forEach(launcherList::add); ServiceLoader.load(LauncherService.class).forEach(launcherList::add);
launcherList.stream().sorted(Comparator.comparing(LauncherService::getOrder)).collect(Collectors.toList()) launcherList.stream()
.sorted(Comparator.comparing(LauncherService::getOrder))
.forEach(launcherService -> launcherService.launcher(builder, appName, profile)); .forEach(launcherService -> launcherService.launcher(builder, appName, profile));
return builder; return builder;
} }