如何使用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-boot:run
选择开发配置文件。如何选择? - 文档说
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
,并告诉我应用程序未能启动
那么如何解决两个问题呢?谢谢〜
我不确定我完全理解这个问题,但我会尝试通过提供一些细节来回答Spring Boot中的配置文件。
对于您的#1示例,根据文档,您可以使用 -Drun使用Spring Boot Maven插件选择配置文件.profiles
。
编辑:对于Spring Boot 2.0+ 运行
已重命名为 spring-boot.run
mvn spring-boot:run -Drun.profiles = dev
http://docs.spring.io/spring-boot/docs/current/ 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
提供的相同值,但来自<$ c的值将加载$ c> yml 文件。 还有多种方法可以定义活动配置文件。
您可以像运行jar一样使用 -Dspring.profiles.active
来定义它们。您还可以使用 SPRING_PROFILES_ACTIVE
环境变量或 spring.profiles.active
系统属性设置配置文件。
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
mvn spring-boot:run -Drun.profiles=dev
http://docs.spring.io/spring-boot/docs/current/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: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles
这篇关于如何使用Spring Boot配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!