Various switching application.properties for each environment when Spring Boot starts

Thing you want to do

Generally, the configuration around application.properties of Spring Boot is as follows.

無題.png

With this configuration, try switching application.properties for each environment when Spring Boot starts. Since there are multiple setting methods, four are introduced here.

1. Set the profile in the startup argument

There are the following two types of methods to set in the startup argument.

1. Command line arguments
java -jar spring-boot-application-properties-sample-1.0.0.jar --spring.profiles.active=dev1
  1. Java System properties (System.getProperties())
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).

2. Profile settings in OS environment variables

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.

1.png

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

3. Profile settings in JNDI on the AP server

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.

Defined in tomcat context.xml

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.

2.png

4. Set profile as environment variable in startup.bat (sh) of AP server

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.

reference

Switch application.properties to be included in WAR with Spring Boot at build time for each environment

that's all.

Recommended Posts

Various switching application.properties for each environment when Spring Boot starts
Change the injection target for each environment with Spring Boot 2
Annotation notes when writing tests for Spring Boot
Spring boot development-development environment-
Switch environment with Spring Boot application.properties and @Profile annotation
[Spring Boot] Environment construction (macOS)
Docker × Spring Boot environment construction
Build Spring for Android 2.0.0 environment
Spring Boot for annotation learning
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Spring Boot for the first time
Frequent annotations for Spring Boot tests
Spring profile function, and Spring Boot application.properties
[Spring Boot] Role of each class
Use DBUnit for Spring Boot test
Resolve null pointers for various sessions during MVC testing of Spring boot.
Handle system environment variables in Spring application.properties
Story when moving from Spring Boot 1.5 to 2.1
Spring Boot + Docker Java development environment construction
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
Try Spring Boot 1 (Environment construction ~ Tomcat startup)
Javaw.exe error when starting Spring Boot (STS)
[Error resolution] Occurs when trying to build an environment for spring with docker