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
Environment Specific application.properties file in Spring Boot application
提问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。
- Can i have environment specific properties file like application-${env-value}.properties?
- 我可以拥有特定于环境的属性文件,例如 application-${env-value}.properties 吗?
In above case, env-value will have values as local/devl/test/prod
在上述情况下,env-value 的值将是 local/devl/test/prod
Where to set the env-value file? For local, i can set it as the jvm argument through sts
Who reads the application.properties in Spring Boot application.
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?
Can i use application.properties and application-local.properties file at same time?
在哪里设置环境值文件?对于本地,我可以通过 sts 将其设置为 jvm 参数
谁读取 Spring Boot 应用程序中的 application.properties。
如何加载环境特定的属性文件?例如 - 如果我在环境特定的属性文件中设置数据库 uid、pwd、模式等,在这种情况下,数据源是否能够理解其中的属性?
我可以同时使用 application.properties 和 application-local.properties 文件吗?
回答by M. Deinum
Spring Boot already has supportfor profilebased properties.
Simply add an application-[profile].properties
file and specify the profiles to use using the spring.profiles.active
property.
只需添加一个application-[profile].properties
文件并使用spring.profiles.active
属性指定要使用的配置文件。
-Dspring.profiles.active=local
This will load the application.properties
and the application-local.properties
with the latter overriding properties from the first.
这将加载application.properties
和application-local.properties
,后者覆盖第一个属性。
回答by Tzen
Yes you can. Since you are using spring, check out @PropertySource
anotation.
是的你可以。由于您使用的是 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 将按照我们的意愿启动我们的应用程序。