【发布时间】:2016-11-15 03:50:48
【问题描述】:
我有一个 Spring Boot 应用程序,它将在各种环境中运行,并且根据它运行的环境,它将连接到不同的数据库。我有几个application.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:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
等等。等等
在我的每个环境中,我都有一个名为 MYENV 的环境变量,它设置为它所在的环境类型,例如 local 或 someserver(application-{env}.properties 文件的名称与环境名称)。
如何让 spring boot 读取此环境变量并自动选择正确的.properties 文件?我不想做整个-Dspring.profiles.active=someserver 因为这个包的部署方式(它不会作为一个jar 运行)。
【问题讨论】:
标签: java spring spring-boot