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));
// 交集
presetProfiles.retainAll(profiles);
// 当前使用
List<String> activeProfileList = new ArrayList<>(profiles);
Function<Object[], String> joinFun = StringUtils::arrayToCommaDelimitedString;
SpringApplicationBuilder builder = new SpringApplicationBuilder(source);
String profile;
if (activeProfileList.isEmpty()) {
// 默认dev开发
if(presetProfiles.isEmpty()){
// 未指定环境默认dev
profile = AppConstant.DEV_CODE;
activeProfileList.add(profile);
builder.profiles(profile);
} else if (activeProfileList.size() == 1) {
profile = activeProfileList.get(0);
profiles.add(profile);
} else if (presetProfiles.size() == 1) {
// 已指定唯一环境
profile = presetProfiles.get(0);
} else {
// 同时存在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 activePros = joinFun.apply(activeProfileList.toArray());
String activePros = joinFun.apply(profiles.toArray());
System.out.println(String.format("----启动中,读取到的环境变量:[%s]jar地址:[%s]----", activePros, startJarPath));
Properties props = System.getProperties();
props.setProperty("spring.application.name", appName);
@ -102,7 +104,8 @@ public class BladeApplication {
// 加载自定义组件
List<LauncherService> launcherList = new ArrayList<>();
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));
return builder;
}