[英]Set Spring Boot application.properties based on Environment Variable
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. 我有一个将在各种环境中运行的Spring Boot应用程序,并且根据它运行的环境,它将连接到不同的数据库。 I have a few
application.properties
files, one for each environment which look like such: 我有一些
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
或someserver
( application-{env}.properties
文件的名称与环境名称完全匹配)。
How can I get spring boot to read this environment variable and automatically choose the correct .properties
file? 如何让spring boot读取此环境变量并自动选择正确的
.properties
文件? 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). 我不想做整个
-Dspring.profiles.active=someserver
因为这个包的部署方式(它不会作为jar运行)。
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 Profile from the MYENV
property or environment variable. 告诉Spring Boot从
MYENV
属性或环境变量中选择Active Profile 。 One way of doing this is to add the following into your application.properties
: 一种方法是将以下内容添加到
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. 如果您不想通过
-D
参数传递系统属性,则可以将相应的环境变量用于spring.profiles.active
。 That corresponding environment variable is SPRING_PROFILES_ACTIVE
. 相应的环境变量是
SPRING_PROFILES_ACTIVE
。 For example if you set the SPRING_PROFILES_ACTIVE
to local
, the local
profile will be activated. 例如,如果将
SPRING_PROFILES_ACTIVE
设置为local
,则将激活local
配置文件。 If you're insisting to use MYENV
environment variable, the first solution is the way to go. 如果你坚持使用
MYENV
环境变量,第一个解决方案就是要走的路。
If you are deploying that application to some container (tomcat, weblogic, ...) you can specify environment variable. 如果要将该应用程序部署到某个容器(tomcat,weblogic,...),则可以指定环境变量。 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}
例如,让我们指定环境变量
application1.spring.profiles.active=someserver
,然后在你的application.properties中设置属性spring.profiles.active=${application1.spring.profiles.active}
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上下文5.0 而不是Spring引导 - 我相信这也适用于Spring 4.1
This is the best solution I believe for my AWS Lambda - with minimal dependencies 这是我相信我的AWS Lambda的最佳解决方案 - 具有最小的依赖性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.