Excerpt from: Antetokounmpo

1. Multiple configuration files

In real development, projects are often deployed in different environments, such as development environment, test environment, production environment, etc., and different environments require different configurations. For example, different environments have different database connection configurations. The configuration we expect to achieve is:

  • Reduce the number of configuration modifications
  • Convenient environment configuration switch

The default configuration file for Spring Boot is application.properties(or YML). So how do you implement different configuration files for different environments? A good practice is to define different configuration files for different environments, as shown below:

Global configuration file: application.yml

Development environment configuration file: application-dev.yml

Test environment configuration file: application-test.yml

Production environment configuration file: application-prod.yml

Second, the way to switch the environment

1. Configure application.yml

Application. Yml is the default configuration file to use, where spring.profiles. Active sets which profile to use, and the following code represents using application-prod.yml:

The configuration file to use
spring:
  profiles:
    active: prod
Copy the code

Note: If application-prod.yml and application-. yml are configured with the same configuration, such as the run port, application-prod.yml has a higher priority.

2. The IDEA of configuration

Click on the IDEA window Run to open Edit Configuration and the Run/Debug Configurations screen appears:

  • VM options sets the startup parameter -dspring.profiles. active=prod

  • Program Arguments setting –spring.profiles.active=prod

  • Active Profile Sets proD

Note: Do not set these three parameters at the same time. Otherwise, conflicts may occur.

3. On the CLI

Type the project into a JAR package, open the command line in the jar package directory, and run the following command to start the project (using the production environment configuration file) :

java -jar spring-boot-profile.jar --spring.profiles.active=prod
Copy the code