Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use Spring Boot 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~

like image 831
twogoods Avatar asked Oct 15 '16 15:10

twogoods


People also ask

What are spring profiles used for?

Spring Profiles helps to easily set right configurations on right environments. Otherwise, without having Spring Profiles it is a big pain to manage environment specific configurations. For example, your application may have to rely on the configurations externalised on the environments.

What is spring boot profile?

Spring Boot profiles A profile is a set of configuration settings. Spring Boot allows to define profile specific property files in the form of application-{profile}. properties . It automatically loads the properties in an application.

What is the benefit of using spring boot profiles?

Spring Profiles provides an efficient way to bundle properties according to functionality, thus freeing the Developer from the tedium of other solutions to problem solve a growing set of application properties.


1 Answers

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

like image 96
francis Avatar answered Oct 01 '22 05:10

francis