[英]Spring boot and configuration profile
I have a Spring Boot application with some configuration. 我有一些配置的Spring Boot应用程序。 I want to specify different configuration values, for
dev
and production
. 我想为
dev
和production
指定不同的配置值。 My application.properties
file look like this: 我的
application.properties
文件如下所示:
spring.datasource.url=jdbc:mysql://devhost:devport/devschema
spring.datasource.username=devuser
spring.datasource.password=mypwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
This file is in my git repo on Openshift. 这个文件在Openshift上我的git repo中。 Everytime I commit, Openshift start to deploy the application, so I need the
application.properties
with the correct values. 每次我提交时,Openshift都会开始部署应用程序,所以我需要带有正确值的
application.properties
。 Is it possible to use something like profiles to change the configuration? 是否可以使用类似配置文件来更改配置?
Create different property files for different profiles using application-{profile}.properties
format, eg application-dev.properties
for dev
, and put your profile specific configurations in them. 使用
application-{profile}.properties
格式为不同的配置文件创建不同的属性文件,例如dev
application-dev.properties
,并将特定于配置文件的配置放入其中。 Then when you're running the spring boot application, activate your intended profile using the SPRING_PROFILES_ACTIVE
environment variable or spring.profiles.active
system property. 然后,当您运行spring boot应用程序时,使用
SPRING_PROFILES_ACTIVE
环境变量或spring.profiles.active
系统属性激活您的预期配置文件。
Since you're using Openshift, you can export the envirnemnt variable in the build
file in the .openshift/action_hooks
directory. 由于您使用的是Openshift,因此可以在
.openshift/action_hooks
目录中的build
文件中导出envirnemnt变量。 Something like this: 像这样的东西:
export JAVA_OPTS_EXT=-Dspring.profiles.active="dev"
The better approach is to use rhc env set
command for enabling one specific profile: 更好的方法是使用
rhc env set
命令启用一个特定的配置文件:
rhc env set SPRING_PROFILES_ACTIVE=dev -a App_Name
You can read more about using environment variables in openshift here . 您可以在此处阅读有关在openshift中使用环境变量的更多信息 。
You can use the spring.profiles.active
Spring property to pick which application-[environment].properties
file to use. 您可以使用
spring.profiles.active
Spring属性来选择要使用的application-[environment].properties
文件。 Then create multiple files, one for each environment. 然后创建多个文件,每个环境一个文件。 To set the environment just pass this
--spring.profiles.active=development
in your Java arguments. 要设置环境,只需在Java参数中传递
--spring.profiles.active=development
。
There are multiple ways you can do this. 有多种方法可以做到这一点。
Using maven profiles and resource plugins to provide environment or profile specific properties by replacing properties in your application.properties
with your profile specific set up. 使用maven配置文件和资源插件通过使用特定于配置文件的设置替换
application.properties
属性来提供环境或配置文件特定的属性。
Using Spring boots build in support for Externalizing properties environment specific 使用Spring引擎构建支持特定于Externalizing属性的环境
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.