diff --git a/blade-core-launch/src/main/java/org/springblade/core/launch/BladeApplication.java b/blade-core-launch/src/main/java/org/springblade/core/launch/BladeApplication.java index 876d2be..8ce290b 100644 --- a/blade-core-launch/src/main/java/org/springblade/core/launch/BladeApplication.java +++ b/blade-core-launch/src/main/java/org/springblade/core/launch/BladeApplication.java @@ -65,24 +65,26 @@ public class BladeApplication { List presetProfiles = new ArrayList<>(Arrays.asList(AppConstant.DEV_CODE, AppConstant.TEST_CODE, AppConstant.PROD_CODE)); // 交集 presetProfiles.retainAll(profiles); - // 当前使用 - List activeProfileList = new ArrayList<>(profiles); Function 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 { // 同时存在dev、test、prod环境时 - 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 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; }