echo_sxy

echo_sxy

Version Switching - Using Profile

Profile is a commonly used feature in Spring that allows different configuration files to be loaded based on different environment configurations, thereby achieving different configuration logic.

Spring Profile#

In Spring Boot, profiles are implemented through configuration files. Different configuration files can be loaded in different environments to achieve different configuration logic. Specifically, Spring Boot supports the following types of configuration files:

  • application.properties
  • application.yml
  • application-{profile}.properties
  • application-{profile}.yml

application.properties and application.yml are common configuration files that are loaded in all environments. application-{profile}.properties and application-{profile}.yml are configuration files loaded based on different profiles. When the application starts, Spring Boot determines which configuration file to load based on the current environment variable. For example, if the current environment variable is dev, the application-dev.properties or application-dev.yml file will be loaded.

Usage#

In actual development, profiles can be used to achieve the following functionalities:

  • Differentiate between different environments, such as development, testing, and production environments.
  • Configure different database connection information, such as using a local MySQL database in the development environment, a remote MySQL database in the testing environment, and an Alibaba Cloud RDS database in the production environment.
  • Configure different log levels, such as using DEBUG level in the development environment, INFO level in the testing environment, and WARN level in the production environment.

Switching through Configuration Files#

Create three different configuration files in the project's resources directory: application-prod.properties, application-test.properties, and application-dev.properties. The current profile needs to be configured in the project's configuration file using one of the following methods:

  • Configure in the application.properties file.

spring.profiles.active=dev

  • Configure in the startup command.

java -jar myproject.jar --spring.profiles.active=dev

Maven Profile#

The principle of Maven profile is to define a set of profiles, and define different sets of property values in different profiles. By selecting a profile, different sets of properties can take effect.
Add different profiles in the pom.xml file:

Then, configure the following in the pom.xml file. With these configurations, the configuration files under src/main/resources/${profiles.active} will be used.

This allows you to switch profiles in IntelliJ IDEA.
The configuration adds two profiles, dev and prod, with activeByDefault indicating the default selection.

  • Maven Run: Specify the profile in Maven using -P, for example, mvn spring-boot -Pdev.
  • Maven Package: Specify the profile in Maven using -P, for example, mvn package -Pdev -DskipTests.

During the process of building a WAR package, the resource files are processed by the maven-resources-plugin. Configure the following in the build section of the pom.xml file:

Extension#

The maven installation package also contains profiles in the conf/settings.xml configuration file.
As we know, the pom.xml also has the profiles attribute, but the difference between the profiles in settings.xml and the profiles in pom.xml is that the profiles in settings.xml only have four sub-elements: activation, repositories, pluginRepositories, and properties. They are usually used to configure repository information.

Loading...
Ownership of this page data is guaranteed by blockchain and smart contracts to the creator alone.