Java 根据环境变量设置Spring Boot application.properties
我有一个Spring Boot应用程序,它将在各种环境中运行,并且根据它运行的环境,它将连接到不同的数据库。我有几个Java 根据环境变量设置Spring Boot application.properties,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个Spring Boot应用程序,它将在各种环境中运行,并且根据它运行的环境,它将连接到不同的数据库。我有几个application.properties文件,每个环境对应一个文件,如下所示: 应用程序本地属性: spring.datasource.platform=postgres spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=dbuser spring.dat
application.properties
文件,每个环境对应一个文件,如下所示:
应用程序本地属性
:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
spring.profiles.active=${MYENV}
应用程序someserver.properties
:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
spring.profiles.active=${MYENV}
等等等等
在我的每个环境中,我都有一个名为MYENV
的环境变量,它被设置为环境的类型,例如local
或someserver
(应用程序-{env}.properties文件的名称与环境名称完全匹配)
如何让spring boot读取此环境变量并自动选择正确的.properties
文件?我不想因为这个包的部署方式(它不会作为jar运行)而不得不完成整个-Dspring.profiles.active=someserver
如何让spring boot读取此环境变量和
是否自动选择正确的.properties文件
告诉Spring Boot从MYENV
属性或环境变量中选择活动配置文件。一种方法是将以下内容添加到应用程序中。properties
:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
spring.profiles.active=${MYENV}
通过这种方式,Spring Boot将Spring.profiles.active
设置为MYENV
环境变量的值
我不必做整个-Dspring.profiles.active=someserver
由于此包的部署方式(它将不会按
(罐子)
如果不希望通过
-D
参数传递系统属性,则可以将相应的环境变量用于该spring.profiles.active
。相应的环境变量是SPRING\u PROFILES\u ACTIVE
。例如,如果将SPRING\u PROFILES\u ACTIVE
设置为local
,则local
配置文件将被激活。如果您坚持使用MYENV
环境变量,那么第一个解决方案就是要走的路。如果您要将该应用程序部署到某个容器(tomcat、weblogic等),则可以指定环境变量。例如,让我们指定环境变量application1.spring.profiles.active=someserver
,然后在application.properties集合属性spring.profiles.active=${application1.spring.profiles.active}
使用SpringContext 5.0,我已经通过以下注释成功地基于系统环境加载了正确的属性文件
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:application-${MYENV:test}.properties")})
这里从系统环境读取MYENV值,如果系统环境不存在,则将加载默认测试环境属性文件,如果我给出错误的MYENV值,则将无法启动应用程序
注意:对于每个配置文件,您需要维护一个应用程序-[profile].属性文件,尽管我使用了SpringContext 5.0&而不是SpringBoot,但我相信这也适用于Spring4.1
这是我认为对我的AWS Lambda来说最好的解决方案-依赖性最小是否可以有一个“默认”配置,以便在env变量中不匹配任何文件,选择一个标准?另外,将默认和通用配置放入
应用程序.properties
。它总是会被使用,但相对于特定的概要文件,它的优先级较低configurations@AliDehghani我在系统变量中设置了值,但application.properties没有将其拾取。变量名:用户值:pass。在我的app.properties spring.datasource.username=${user}中,这在spring上下文中而不是在spring引导中是可能的……我还需要将该属性保留在application.properties文件中,然后使用活动配置文件覆盖该属性吗?-或者,如果我不将基于ebvironment的属性放在application.properties中,而只是放在它的[profile].properties文件中,我希望依赖性最小—就像我为AWS Lambda编写代码一样—而不是