1. The background

What are the different runtime configurations?

Projects typically have multiple sets of environments, such as:

  • The development environment
  • The test environment
  • UAT test environment
  • Generate environment

In different environments, the configuration of software systems is different. For example, test databases are used in test time and formal data is used in production.

SpringBoot profiles provide convenience by allowing different profiles to be configured in different environments.

2. Profile description

Profiles allow Spring to provide different configurations for different environments, enabling quick switching between environments by activation, specifying parameters, and so on.

In other words, we need to use different configurations in different scenarios, and the emergence of profiles is to solve the problem of complex configuration switching in multiple environments.

Spring’s official syntax rules are:

Application - {profile}. The properties (or yaml /. Yml)Copy the code

3. Apply the Profile configuration

Divided into the following steps:

  • Step 1: Create a configuration file
  • Step 2: Activate the profile

3.1 Step 1: Create a configuration file

We create multiple profiles following the rules above. For example, create the following configuration files for development, test, and release:

  • application-dev.yml
  • application-test.yml
  • application-release.yml

3.2 Step 2: Activate the configuration file

It supports multiple approaches

  • Run as a JAR package
  • Run time in IDEA development IDE
  • The @profile annotation is used in the code to distinguish

Running in jar mode When running in JAR mode we can specify the configuration file to be loaded by the program. Specify the following parameters:

--spring.profiles.active=prod
Copy the code

Complete example:

java -jar -Dspring.profiles.active=prod *.jar
Copy the code

(2) While running in IDEaDeveloper IDE, click Edit, Hoisting… Find Activte Profiles and fill in the name. See below.

image.png

(3) The annotation @profile is used in the code to distinguish

Using the @profile annotation, you can specify that a class or method is valid for a particular Profile environment.

END