如何使用 Spring Boot 配置文件 [英] how to use Spring Boot profiles
问题描述
我有application.yml
、application-dev.yml
和application-dev.yml
- 我正在使用 maven 命令
mvn spring-boot:run -Dspring.profiles.active=dev
它不起作用,我无法使用mvn spring 选择开发配置文件-引导:运行
.我该如何选择? - 文档说
java -jar XXX.jar --spring.profiles.active=dev
有效,我尝试了-Dspring.profiles.active=dev
但它不起作用.在我的项目中,我使用java -jar XXX.jar
它运行,但如果我使用java -jar XXX.jar --spring.profiles.active=dev
选择开发配置文件,控制台打印这么多日志并警告我从未见过使用过的java -jar XXX.jar
,并告诉我APPLICATION FAILED TO START
那么如何解决两个问题呢?谢谢~
我不确定我是否完全理解这个问题,但我会尝试通过提供有关 Spring Boot 中的配置文件的一些细节来回答.
对于您的 #1 示例,根据文档,您可以使用 Spring Boot Maven 插件使用 -Drun.profiles
选择配置文件.
编辑:对于 Spring Boot 2.0+ run
已重命名为 spring-boot.run
和 run.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.yml
和 application-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.yml
andapplication-dev.yml
- 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 usingmvn spring-boot:run
. How do I choose it? - 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 usejava -jar XXX.jar
it runs, but if I usejava -jar XXX.jar --spring.profiles.active=dev
to choose dev profile, console print so many logs and warns that i never see usedjava -jar XXX.jar
,and tell meAPPLICATION 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
这篇关于如何使用 Spring Boot 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!