Java 根据环境变量设置 Spring Boot application.properties
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38336753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Spring Boot application.properties based on Environment Variable
提问by Tanishq dubey
I have a Spring Boot application that will run in various environments, and based on the environment it runs in, it will connect to a different database. I have a few application.properties
files, one for each environment which look like such:
我有一个 Spring Boot 应用程序,它将在各种环境中运行,并且根据它运行的环境,它将连接到不同的数据库。我有几个application.properties
文件,每个环境一个,看起来像这样:
application-local.properties
:
application-local.properties
:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789
application-someserver.properties
:
application-someserver.properties
:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
etc. etc.
等等等等
On each of my environments, I have a environment variable called MYENV
which is set to the type of environment it is, for example local
or someserver
(the name of the application-{env}.properties
files perfectly matches the environment name).
在我的每个环境中,我都有一个名为的环境变量MYENV
,它被设置为环境类型,例如local
or someserver
(application-{env}.properties
文件名与环境名完全匹配)。
How can I get spring boot to read this environment variable and automatically choose the correct .properties
file? I don't want to have to do the whole -Dspring.profiles.active=someserver
because of the way this package is deployed (it will not be running as a jar).
如何让 spring boot 读取这个环境变量并自动选择正确的.properties
文件?-Dspring.profiles.active=someserver
由于这个包的部署方式(它不会作为 jar 运行),我不想做整个事情。
采纳答案by Ali Dehghani
How can I get spring boot to read this environment variable and automatically choose the correct .properties file?
如何让 spring boot 读取这个环境变量并自动选择正确的 .properties 文件?
Tell Spring Boot to select the Active Profilefrom the MYENV
property or environment variable. One way of doing this is to add the following into your application.properties
:
告诉 Spring Boot从属性或环境变量中选择Active ProfileMYENV
。一种方法是将以下内容添加到您的application.properties
:
spring.profiles.active=${MYENV}
This way Spring Boot will set spring.profiles.active
to the value of MYENV
environment variable.
这样 Spring Boot 将设置spring.profiles.active
为MYENV
环境变量的值。
I don't to have to do the whole -Dspring.profiles.active=someserver because of the way this package is deployed (it will not be running as a jar)
由于这个包的部署方式,我不必做整个 -Dspring.profiles.active=someserver (它不会作为 jar 运行)
You can use the corresponding environment variable to that spring.profiles.active
, if you don't like to pass a system property through -D
arguments. That corresponding environment variable is SPRING_PROFILES_ACTIVE
. For example if you set the SPRING_PROFILES_ACTIVE
to local
, the local
profile will be activated. If you're insisting to use MYENV
environment variable, the first solution is the way to go.
spring.profiles.active
如果您不喜欢通过-D
参数传递系统属性,您可以使用相应的环境变量。相应的环境变量是SPRING_PROFILES_ACTIVE
. 例如,如果您将 设置SPRING_PROFILES_ACTIVE
为local
,则local
配置文件将被激活。如果你坚持使用MYENV
环境变量,第一个解决方案就是要走的路。
回答by bilak
If you are deploying that application to some container (tomcat, weblogic, ...) you can specify environment variable. For example lets specify environment variable application1.spring.profiles.active=someserver
and then in your application.properties set property spring.profiles.active=${application1.spring.profiles.active}
如果您将该应用程序部署到某个容器(tomcat、weblogic 等),您可以指定环境变量。例如让我们指定环境变量application1.spring.profiles.active=someserver
,然后在您的 application.properties 设置属性spring.profiles.active=${application1.spring.profiles.active}
回答by Abdeali Chandanwala
Using Spring context 5.0 I have successfully achieved loading correct property file based on system environment via the following annotation
使用 Spring context 5.0 我已经通过以下注释成功实现了基于系统环境加载正确的属性文件
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:application-${MYENV:test}.properties")})
Here MYENV value is read from system environment and if system environment is not present then default test environment property file will be loaded, if I give a wrong MYENV value - it will fail to start the application.
这里 MYENV 值是从系统环境中读取的,如果系统环境不存在,则将加载默认的测试环境属性文件,如果我给出了错误的 MYENV 值 - 它将无法启动应用程序。
Note: for each profile, you want to maintain you need to make an application-[profile].property file and although I used Spring context 5.0 & not Spring boot- I believe this will also work on Spring 4.1
注意:对于每个配置文件,您想要维护您需要创建一个 application-[profile].property 文件,尽管我使用的是 Spring context 5.0而不是 Spring boot- 我相信这也适用于 Spring 4.1
This is the best solution I believe for my AWS Lambda - with minimal dependencies
这是我认为适用于我的 AWS Lambda 的最佳解决方案 - 依赖最少