From b6e6cc27243651b0f695575dfcae65876a24bfad Mon Sep 17 00:00:00 2001 From: tangjl Date: Fri, 7 Jul 2023 12:45:07 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E9=80=BB=E8=BE=91=E5=AF=BC=E8=87=B4spring=20?= =?UTF-8?q?boot=E5=A4=9Aprofiles=E5=8A=9F=E8=83=BD=E4=B8=8D=E5=8F=AF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98=20#=20=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0=20=E6=A1=86=E6=9E=B6=E5=9C=A8=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=BA=94=E7=94=A8=E5=90=AF=E5=8A=A8=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=9A=84profiles=E6=97=B6=EF=BC=8C=E6=9C=AC=E6=84=8F=E4=B8=BA?= =?UTF-8?q?=E4=B8=8D=E5=85=81=E8=AE=B8=E6=8C=87=E5=AE=9A=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=EF=BC=8C=E5=8D=B3=E4=B8=8D=E5=8F=AF=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E6=8C=87=E5=AE=9Adev=E3=80=81test=E3=80=81prod?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E4=B8=A4=E4=B8=AA=E6=88=96=E4=B8=89=E4=B8=AA?= =?UTF-8?q?=EF=BC=8C=E6=9C=80=E5=A4=9A=E5=8F=AA=E5=85=81=E8=AE=B8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=85=B6=E4=B8=AD=E4=B8=80=E4=B8=AA=E3=80=82=E4=BD=86?= =?UTF-8?q?=E5=85=B7=E4=BD=93=E5=AE=9E=E7=8E=B0=E5=8D=B4=E6=98=AF=E4=B8=8D?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=8C=87=E5=AE=9A=E5=A4=9A=E4=B8=AAprofile?= =?UTF-8?q?=EF=BC=8C=E5=8D=B3=E4=BE=BF=E4=B8=8D=E6=98=AF=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E7=9A=84profile=E4=B9=9F=E4=B8=8D=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E5=AD=98=E5=9C=A8=E5=A4=9A=E4=B8=AA=E3=80=82=E8=BF=99?= =?UTF-8?q?=E5=B0=B1=E5=AF=BC=E8=87=B4=E6=9C=AC=E6=9D=A5spring=20boot?= =?UTF-8?q?=E6=89=80=E5=85=B7=E6=9C=89=E7=9A=84=E5=90=8C=E6=97=B6=E6=BF=80?= =?UTF-8?q?=E6=B4=BB=E5=A4=9A=E4=B8=AAprofile=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E7=94=A8=E4=BA=86=EF=BC=8C=E4=B8=8Espring=20?= =?UTF-8?q?boot=E6=A1=86=E6=9E=B6=E5=86=B2=E7=AA=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 解决方案 检测应用所设置profiles时,只要求不能使用dev、test、prod的多个,其它profile允许多个。比如`spring.profiles.active=dev,mysql,redis` Signed-off-by: tangjl --- .../core/launch/BladeApplication.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) 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; }