简体   繁体   English

根据环境变量设置Spring Boot application.properties

[英]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的环境变量,它被设置为环境类型,例如localsomeserverapplication-{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.

相关问题 基于application.properties的Spring Boot和EntityManager - Spring boot and EntityManager based on application.properties 覆盖 spring boot application.properties 的系统环境属性 - System environment properties overriding spring boot application.properties Spring Boot:如何在 application.properties 中指定带有破折号的环境变量? - Spring Boot: How do you specify an environment variable that has dashes in the application.properties? Springboot with Docker: environment variable to override RabbitMQ host IP property in spring boot's application.properties is not working - Springboot with Docker: environment variable to override RabbitMQ host IP property in spring boot's application.properties is not working 如果未在application.properties中显式设置键,是否不可能通过环境变量“覆盖” Spring属性? - Is it not possible to “override” Spring properties through environment variable if the key are not explicitly set in application.properties? spring boot 没有为环境加载正确的 Jasypt application.properties - spring boot not loading correct Jasypt application.properties for environment spring-boot application.properties中的环境变量错误 - Environment Variables in spring-boot application.properties error application.properties 中电子邮件数据中 Spring-Boot 中的环境变量 - environment variables in Spring-Boot in email data in application.properties 如何在 spring 引导中使用带有 application.properties 的环境变量? - How to use environment variables with application.properties in spring boot? 类中的访问基于环境的 application.properties 文件不受 spring 管理 - Access Environment based application.properties file in class not managed by spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM