简体   繁体   English

使用 Gradle 从 Spring Boot 中的环境变量设置 spring.profiles.active

[英]Setting spring.profiles.active from an environment variable in Spring Boot with Gradle

My company has a standard way to specifying the environment a web service should run in, specifically 'development' and 'production' by using an environment variable APP_ENV.我的公司有一种标准方法来指定 Web 服务应该运行的环境,特别是通过使用环境变量 APP_ENV 来指定“开发”和“生产”。 Each of these environments have a separate config file: application-dev.yml and application-prod.yml.这些环境中的每一个都有一个单独的配置文件:application-dev.yml 和 application-prod.yml。

In spring boot, we can use SPRING_PROFILES_ACTIVE or equivalents to do this, but what I really want to do is grab the VALUE from APP_ENV, and use that as the profile.在 spring boot 中,我们可以使用 SPRING_PROFILES_ACTIVE 或等效项来执行此操作,但我真正想做的是从 APP_ENV 中获取 VALUE,并将其用作配置文件。

I've got some code我有一些代码

SpringApplication app = new SpringApplication(Application.class);
springApp.setAdditionalProfiles(System.getenv("APP_ENV"););
springApp.run(args);

Any tips on how to do this a bit cleaner?关于如何做到这一点更清洁的任何提示? I'm getting java.lang.IllegalStateException: Failed to load ApplicationContext errors on various gradle tasks that make this solution kind of flimsy.我收到java.lang.IllegalStateException: Failed to load ApplicationContext各种 gradle 任务上的错误,这使得这个解决方案有点脆弱。

you can use System.getProperty which will always return a String like below,您可以使用 System.getProperty 它将始终返回如下所示的字符串,

springApp.setAdditionalProfiles(System.getProperty("APP_ENV", "dev"));

In the above snippet, we use System.getProperty(“APP_ENV”) to extract the value of the property APP_ENV.在上面的代码片段中,我们使用 System.getProperty(“APP_ENV”) 来提取属性 APP_ENV 的值。 We also are making use of the default value so if the property is not available in the system, getProperty returns dev.我们还使用了默认值,因此如果该属性在系统中不可用,则 getProperty 返回 dev。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot在环境中设置活动配置文件 - Spring Boot setting up Active profiles in an environment Spring 引导获取 static 块中的 spring.profiles.active 值 - Spring Boot get spring.profiles.active value in a static block 春季启动:spring.profiles.active = dev / test / prod - Spring Boot: spring.profiles.active=dev/test/prod Spring 引导 2.5.6 捕获 22 与 spring.profiles.active - Spring Boot 2.5.6 catch 22 with spring.profiles.active spring.profiles.active 不兑现 - spring.profiles.active not honoured Gradle bootRun 不适用于 SPRING_PROFILES_ACTIVE 但适用于 spring.profiles.active - Gradle bootRun not working with SPRING_PROFILES_ACTIVE but works with spring.profiles.active spring.profiles.active 在springboot应用程序中不起作用 - spring.profiles.active is not working in springboot application 在测试中的Spring Environment中将@EnabledIf与spring.profiles.active属性一起使用 - Using @EnabledIf with spring.profiles.active property in Spring Environment within tests "无法解析值“classpath:\/ldap-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”" - Could not resolve placeholder 'spring.profiles.active' in value "classpath:/ldap-${spring.profiles.active}.properties" 为什么 spring.profiles.active 只读取部分配置文件? - Why does spring.profiles.active only read some profiles?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM