Spring Boot 应用程序中特定于环境的 application.properties 文件 [英] Environment Specific application.properties file in Spring Boot application
问题描述
在我的 Spring Boot 应用程序中,我想创建特定于环境的属性文件.我的应用程序在 war 中的打包类型,我正在嵌入式 tomcat 中执行它.我使用 sts 并从 sts 本身执行 main.
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.
- 我可以拥有特定于环境的属性文件,例如 application-${env-value}.properties 吗?
在上述情况下,env-value 的值为 local/devl/test/prod
In above case, env-value will have values as local/devl/test/prod
在哪里设置 env-value 文件?对于本地,我可以通过 sts 将其设置为 jvm 参数
Where to set the env-value file? For local, i can set it as the jvm argument through sts
谁读取 Spring Boot 应用程序中的 application.properties.
Who reads the application.properties in Spring Boot application.
如何加载环境特定的属性文件?例如 - 如果我在环境特定的属性文件中设置数据库 uid、pwd、schema 等,在这种情况下,数据源是否能够理解其中的属性?
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?
我可以同时使用 application.properties 和 application-local.properties 文件吗?
Can i use application.properties and application-local.properties file at same time?
推荐答案
Spring Boot 已经 支持 profile 基于属性.
Spring Boot already has support for profile based properties.
只需添加一个 application-[profile].properties
文件并使用 spring.profiles.active
属性指定要使用的配置文件.
Simply add an application-[profile].properties
file and specify the profiles to use using the spring.profiles.active
property.
-Dspring.profiles.active=local
这将加载 application.properties
和 application-local.properties
,后者覆盖第一个属性.
This will load the application.properties
and the application-local.properties
with the latter overriding properties from the first.
这篇关于Spring Boot 应用程序中特定于环境的 application.properties 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!