Detailed explanation of the use of spring.profiles

This article explains the usage and differences between spring.profiles.active and spring.profiles.include

Article Directory

  • Business scene
  • spring.profiles.active property
    • Specified at startup
  • spring.profiles.includeProperty
    • Configuration method
  • Configuration location
  • Configuration differences
  • Use and differentiate with examples
  • *Test 1
  • Test 2
  • Test three
  • Write a program to view the activated yml file
  • Summary of this article

Business scene

When we develop Spring Boot applications, the same set of programs is usually applied and installed in several different environments, such as development, testing, production, etc. The database address, server port, etc. configuration of each environment will be different. If the configuration files have to be modified frequently when packaging for different environments, it will be a very tedious and error-prone task.

For the configuration of multiple environments, the basic idea of ​​various project construction tools or frameworks is the same. By configuring multiple configuration files for different environments, and then specifying the content to be packaged through the packaging command, the packaging is differentiated. Spring Boot is no exception. , or simpler.


spring.profiles.active property

A good way is to create different configuration files, and the naming rules follow application-${profile}.yml, for example:

  1. Development environment configuration file: application-dev.yml
  2. Test environment configuration file: application-test.yml
  3. Production environment configuration file: application-prod.yml

Of course, we cannot delete the top-level application.yml configuration of the project. In this file, different configuration files are switched according to different deployment scenarios: configure spring.profiles.active, and the attribute value is ${profile}.

  1. spring.profiles.active=dev:enableapplication-dev.yml
  2. spring.profiles.active=test:enableapplication-test.yml
  3. spring.profiles.active=prod:enableapplication-prod.yml

Specified at startup

When executing parameterized startup, you can specify the configuration file to be selected in the command, for example: java -jar xx.jar –spring.profiles.active=test

This command has the highest priority. Even if spring.profiles.active=dev has been configured in application.yml, the final program will still use the application-test.yml configuration file.


spring.profiles.includeProperty

Going one step further, for the development environment, you want to use different configuration files to store different configurations of the development environment, for example:

  • Store jdbc information in application-dev1.yml
  • Store ip and port information in application-dev2.yml

That is, when enabling the application-dev.yml development environment (main) configuration file, enable application-dev1.yml and application-dev2.yml at the same time.

Then, you can use the spring.profiles.include attribute: enable other profiles at the same time


Configuration method

If it is a properties file: spring.profiles.include=dev1,dev2

If it is in the yaml file,
spring.profiles.include:
-dev1
-dev2
Or: spring.profiles.include:dev1,dev2


Configuration location

  1. Configuration method 1: In application.properties, configure spring.profiles.active=dev and specify spring.profiles.include=dev1,dev2 at the same time.
  2. Configuration method two: In application.properties, configure spring.profiles.active=dev, in application-dev.properties, configure spring.profiles.include=dev1,dev2. When using application-dev.properties, the two files dev1 and dev2 are automatically activated without specifying them again. (Personally I think the second method is better)

Configuration differences

Configuration differences

  1. When starting in the first way, the console prints The following profiles are active:dev1,dev2,dev
  2. When starting in the second method, the console prints The following profiles are active:dev, dev1,dev2

In order, the later ones cover the previous ones.


Use and differentiate with examples

application.yml placement:

server:
  port: 8088

#What is activated is the development environment configuration file
spring:
    profiles:
       active: dev 

application-dev.yml placement:

server:
  port: 8089

#trigger simultaneouslydev1anddev2activation
spring:
  profiles:
    include: dev1,dev2 

application-dev1.yml placement:

server:
  port: 8090 

application-dev2.yml placement:

server:
  port: 8091 

application-prod.yml placement:

server:
  port: 9088

#trigger simultaneouslyprod1andprod2activation
spring:
  profiles:
    include: prod1,prod2 

application-prod1.yml configuration:

server:
  port: 9089 

application-prod2.yml placement:

server:
  port: 9090 

Test one

In the application.yml configuration file, specify

#What is activated is the development environment configuration file
spring:
    profiles:
       active: dev 

Start without specifying parameters

Insert image description here

Start port 8091 in dev2

Insert image description here


Test 2

Based on test 1, specify parameters at startup: –spring.profiles.active=prod. The highest priority, the production environment configuration file will be used

Insert image description here

Start results

Insert image description here


Test three

In the application.yml configuration file, specify

#What is activated is the development environment configuration file
spring:
    profiles:
       active: dev
       include: dev1,dev2 

Start results

Insert image description here


Write a program to view the activated yml file

Write a program to view the activated yml file

package com.wideth;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@Slf4j
@SpringBootApplication
public class AppApplication {

    public static void main(String[] args) {

        try {
            ApplicationContext ctx = SpringApplication.run
            (AppApplication.class, args);
            String[] activeProfiles = ctx.getEnvironment().
            getActiveProfiles();
            for (String profile : activeProfiles) {
                log.warn("Spring Boot useprofilefor:{}" , profile);
            }
        } catch (Exception e) {
            log.error("Startup failed!",e);
        }

    }

} 

Program results


Summary of this article

This article introduces the use of spring.profiles and related knowledge