Generally, the configuration around application.properties of Spring Boot is as follows.
With this configuration, try switching application.properties for each environment when Spring Boot starts. Since there are multiple setting methods, four are introduced here.
There are the following two types of methods to set in the startup argument.
java -jar spring-boot-application-properties-sample-1.0.0.jar --spring.profiles.active=dev1
java -jar -Dspring.profiles.active=dev1 spring-boot-application-properties-sample-1.0.0.jar
However, https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html As you can see, the command line arguments have a higher priority, so if you start with both settings as shown below,
$ java -jar -Dspring.profiles.active=dev1 spring-boot-application-properties-sample.jar --spring.profiles.active=dev2
Java System properties (System.getProperties ())-Dspring.profiles.active = dev1 not, Command line arguments --spring.profiles.active = dev2
Note that is adopted (overwritten).
Spring Boot loads the environment variable "SPRING_PROFILES_ACTIVE" at startup and sets it as a profile. Therefore, if you define "SPRING_PROFILES_ACTIVE" in the environment variable of the OS in advance, the profile defined there will be adopted.
Windows settings.
Linux settings.
.
export SPRING_PROFILES_ACTIVE=dev1
If you define it as above, the profile called dev1 will be loaded when Spring Boot starts, and "application-dev1.properties" will be adopted. The following is the console output at that time. At the bottom, "The following profiles are active: dev1" is output, indicating that dev1 is active.
console(Excerpt).log
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.0.RELEASE)
2017-08-20 20:27:41.807 INFO 7080 --- [ main] com.example.App : Starting App on N-PC with PID 7080 (C:\Development\sts-bundle\workspace\spring-boot-application-properties-sample\target\classes started by N in C:\Development\sts-bundle\workspace\spring-boot-application-properties-sample)
2017-08-20 20:27:41.807 INFO 7080 --- [ main] com.example.App : The following profiles are active: dev1
In the case where the application created by Spring Boot is set to WAR and deployed to another AP server to operate, JNDI of the AP server will set it as a profile. The following is a setting example when using Tomcat.
context.xml
<?xml version="1.0" encoding="utf-8"?>
<Context>
<Environment
type="java.lang.String"
name="spring.profiles.active"
value="dev2"/>
</Context>
If you define it as above, the profile called dev2 will be loaded when Tomcat starts, and "application-dev2.properties" will be adopted. The following is the console output at that time. At the bottom, "The following profiles are active: dev2" is output, indicating that dev2 is active.
For Tomcat, startup.bat (sh), catalina.bat (sh), etc.
startup.bat
set "SPRING_PROFILES_ACTIVE=dev2"
startup.sh
export SPRING_PROFILES_ACTIVE=dev2
It is also possible to specify a profile in the environment variable "SPRING_PROFILES_ACTIVE" as in. In this case, in context.xml,
context.xml
<?xml version="1.0" encoding="utf-8"?>
<Context>
<Environment
type="java.lang.String"
name="spring.profiles.active"
value="dev3"/>
</Context>
If it is defined as, that profile (dev3 defined in context.xml) has priority.
that's all.
Recommended Posts