Spring Boot: 如何创建3个配置文件并在Spring Boot中实现

huangapple go评论55阅读模式
英文:

Spring Boot: How to create 3 profiles and implement in spring boot

问题

我想创建3个用于访问数据库的配置文件,根据环境不同分为以下三种:

  1. application_prod.properties - 应包含所有与生产环境相关的属性。
  2. application_qlty.properties - 应包含所有与质量环境相关的属性。
  3. application_dev.properties - 应包含所有与开发环境相关的属性。

如何在Spring Boot中实现上述3个配置文件,以及如何根据环境类型选择配置文件?如何以简便的方式最佳实践地实现上述内容?

英文:

I want to create 3 profiles to access the database based on the enviroment

Instead of writing all the properties in a single file. I want the properties has to be split into 3 based on enviroment (Qlty,Prod,Dev). That is

1.application_prod.properties --should contain all production related details
2.application_qlty.properties --should contain all qlty related details
3.application_dev.properties  --should contain all dev related details

How to implment the above 3 profiles in spring boot and how to select the profile based on the type of enviroment. what is the best practice to implement the above in an easy manner

答案1

得分: 1

你只需在运行选项中添加配置文件:

java -jar app.jar --spring.profiles.active=dev

或者使用Maven:

mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

值得一提的是,配置文件的名称使用短横线 "-":

application-dev.properties
英文:

you just add profile on run options

java -jar app.jar --spring.profiles.active=dev

or with maven

mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

One remark. profile on property file writes with "-"

application-dev.properties

huangapple
  • 本文由 发表于 2023年2月6日 13:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定