Spring Boot 应用程序中特定于环境的 application.properties 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32196451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:45:16  来源:igfitidea点击:

Environment Specific application.properties file in Spring Boot application

springspring-boot

提问by user3534483

In my Spring Boot application, i want to create environment specific properties file. The packaging type of my application in war and i am executing it in embedded tomcat. I use sts and execute the main from sts itself.

在我的 Spring Boot 应用程序中,我想创建特定于环境的属性文件。我的应用程序在 war 中的打包类型,我正在嵌入式 tomcat 中执行它。我使用 sts 并从 sts 本身执行 main。

  1. Can i have environment specific properties file like application-${env-value}.properties?
  1. 我可以拥有特定于环境的属性文件,例如 application-${env-value}.properties 吗?

In above case, env-value will have values as local/devl/test/prod

在上述情况下,env-value 的值将是 local/devl/test/prod

  1. Where to set the env-value file? For local, i can set it as the jvm argument through sts

  2. Who reads the application.properties in Spring Boot application.

  3. How to load the environment specific properties file? For ex - if i set the database uid,pwd, schema etc in environment specific property file, in that case will the datasource be able to understand the properties in it?

  4. Can i use application.properties and application-local.properties file at same time?

  1. 在哪里设置环境值文件?对于本地,我可以通过 sts 将其设置为 jvm 参数

  2. 谁读取 Spring Boot 应用程序中的 application.properties。

  3. 如何加载环境特定的属性文件?例如 - 如果我在环境特定的属性文件中设置数据库 uid、pwd、模式等,在这种情况下,数据源是否能够理解其中的属性?

  4. 我可以同时使用 application.properties 和 application-local.properties 文件吗?

回答by M. Deinum

Spring Boot already has supportfor profilebased properties.

Spring Boot 已经支持基于配置文件的属性。

Simply add an application-[profile].propertiesfile and specify the profiles to use using the spring.profiles.activeproperty.

只需添加一个application-[profile].properties文件并使用spring.profiles.active属性指定要使用的配置文件。

-Dspring.profiles.active=local

This will load the application.propertiesand the application-local.propertieswith the latter overriding properties from the first.

这将加载application.propertiesapplication-local.properties,后者覆盖第一个属性。

回答by Tzen

Yes you can. Since you are using spring, check out @PropertySourceanotation.

是的你可以。由于您使用的是 spring,请查看@PropertySource注释。

Anotate your configuration with

注释您的配置

@PropertySource("application-${spring.profiles.active}.properties")

You can call it what ever you like, and add inn multiple property files if you like too. Can be nice if you have more sets and/or defaults that belongs to all environments (can be written with @PropertySource{...,...,...} as well).

您可以随意称呼它,如果您也喜欢,还可以添加多个属性文件。如果您有更多属于所有环境的集合和/或默认值(也可以用 @PropertySource{...,...,...} 编写),那就太好了。

@PropertySources({
  @PropertySource("application-${spring.profiles.active}.properties"),
  @PropertySource("my-special-${spring.profiles.active}.properties"),
  @PropertySource("overridden.properties")})

Then you can start the application with environment

然后您可以使用环境启动应用程序

-Dspring.active.profiles=test

In this example, name will be replaced with application-test-properties and so on.

在此示例中,名称将替换为 application-test-properties 等。

回答by Cuttlefish

we can do like this:

我们可以这样做:

in application.yml:

application.yml 中

spring:
  profiles:
    active: test //modify here to switch between environments
    include:  application-${spring.profiles.active}.yml

in application-test.yml:

application-test.yml 中

server:
  port: 5000

and in application-local.yml:

application-local.yml 中

server:
  address: 0.0.0.0
  port: 8080

then spring boot will start our app as we wish to.

然后 spring boot 将按照我们的意愿启动我们的应用程序。