环境 VM 参数传递给 bootRun 未被 Spring 引导应用程序拾取



我正在尝试在运行时设置我的 Spring Boot (1.5.4.RELEASE( 应用程序的环境,但似乎我有一些不对齐的地方。

我的应用程序.yml是这样定义的:

spring:
profiles.active: ${env:local}
---
spring:
profiles: local
foo: bar
---
spring:
profiles: dev
foo: bar

在我已注释为 @Configuration 的类中,我有一个执行以下操作的方法,以便我可以显示正在使用的环境:

@Value('${spring.profiles.active}')
String activeProfile
@PostConstruct
def bootComplete() {
println "App started with profile: $activeProfile"
}

在此配置下,当我的应用程序启动时,我在控制台中看到以下内容:

App started with profile: local

如果我在我的 application.yml 中将 ${env:local} 修改为${env:dev}并启动应用程序,我会在控制台中看到以下内容

App started with profile: dev

我的目标是使用 VM 参数启动应用程序,以便在运行时设置活动配置文件。 我正在添加参数:-denv=dev,但它似乎对应用程序的启动没有影响。 谁能建议我在这里可能忽略什么?

我找到了解决问题的方法。 问题是我正在使用 Gradle bootRun 启动应用程序。 我的假设是将使用那里设置的 VM 参数。 我现在通过直接调用类来运行应用程序,并且 VM 参数正在工作(-Denv 和 -Dspring.profiles.active(

执行-Denv=env不会产生任何影响,因为env不是属性键。 这是你可以做到的:

-Dspring.profiles.active=dev

最新更新