且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Spring Boot 配置文件

更新时间:2022-10-17 22:19:18

我不确定我是否完全理解这个问题,但我会尝试通过提供有关 Spring Boot 中的配置文件的一些细节来回答.

对于您的 #1 示例,根据文档,您可以使用 Spring Boot Maven 插件使用 -Drun.profiles 选择配置文件.

编辑:对于 Spring Boot 2.0+ run 已重命名为 spring-boot.runrun.profiles 已重命名为 spring-boot.run.profiles

mvn spring-boot:run -Dspring-boot.run.profiles=dev

https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-profiles.html

从您的 #2 示例中,您正在 jar 名称后定义活动配置文件.您需要在运行的 jar 名称之前提供 JVM 参数.

java -jar -Dspring.profiles.active=dev XXX.jar

一般信息:

您提到您同时拥有 application.ymlapplication-dev.yml.使用dev 配置文件运行实际上会加载两个 配置文件.application-dev.yml 中的值将覆盖 application.yml 提供的相同值,但将加载两个 yml 文件中的值.>

还有多种方法可以定义活动配置文件.

您可以在运行 jar 时使用 -Dspring.profiles.active 定义它们.您还可以使用 SPRING_PROFILES_ACTIVE 环境变量或 spring.profiles.active 系统属性设置配置文件.

更多信息可以在这里找到:https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles

i have application.yml,application-dev.ymlandapplication-dev.yml

  1. I'm using the maven command mvn spring-boot:run -Dspring.profiles.active=dev it doesn't work and I can not choose the dev profile using mvn spring-boot:run. How do I choose it?
  2. The documentation says java -jar XXX.jar --spring.profiles.active=dev works, and I tried -Dspring.profiles.active=dev but it does not work. And in my project, i use java -jar XXX.jar it runs, but if I use java -jar XXX.jar --spring.profiles.active=dev to choose dev profile, console print so many logs and warns that i never see used java -jar XXX.jar,and tell me APPLICATION FAILED TO START

so how to solve two problems? thanks~

I'm not sure I fully understand the question but I'll attempt to answer by providing a few details about profiles in Spring Boot.

For your #1 example, according to the docs you can select the profile using the Spring Boot Maven plugin using -Drun.profiles.

Edit: For Spring Boot 2.0+ run has been renamed to spring-boot.run and run.profiles has been renamed to spring-boot.run.profiles

mvn spring-boot:run -Dspring-boot.run.profiles=dev

https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-profiles.html

From your #2 example, you are defining the active profile after the name of the jar. You need to provide the JVM argument before the name of the jar you are running.

java -jar -Dspring.profiles.active=dev XXX.jar

General info:

You mention that you have both an application.yml and a application-dev.yml. Running with the dev profile will actually load both config files. Values from application-dev.yml will override the same values provided by application.yml but values from both yml files will be loaded.

There are also multiple ways to define the active profile.

You can define them as you did, using -Dspring.profiles.active when running your jar. You can also set the profile using a SPRING_PROFILES_ACTIVE environment variable or a spring.profiles.active system property.

More info can be found here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles