简体   繁体   English

Spring启动和配置文件

[英]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 . 我想为devproduction指定不同的配置值。 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. 有多种方法可以做到这一点。

  1. 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属性来提供环境或配置文件特定的属性。

    This will help you getting started 这将有助于您入门

  2. Using Spring boots build in support for Externalizing properties environment specific 使用Spring引擎构建支持特定Externalizing属性的环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM