[英]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.