SoFunction
Updated on 2025-03-08

Implementation of springboot3 environment isolation

Profiles

Environmental isolation capability; quick switching of development, testing and production environments

step:

  • Identify environment: Specify which components and which environment is configured to take effect {dev development environment test test environment prod production environment default environment }
  • Switch environment: All components and configurations corresponding to this environment should take effect. Only when the corresponding environment is activated will these components take effect.

use

Specify environment

Spring Profiles provides a way to isolate configurations so that they only take effect in specific environments;
Any @Component, @Configuration or @ConfigurationProperties can use the @Profile tag to specify when it is loaded. [All components in the container can be marked by @Profile]

Environmental activation

Configure activation of the specified environment; configuration file

=production,hsqldb

It can also be activated using the command line. --=dev,hsqldb
You can also configure the default environment; components that do not label @Profile will always exist.

  • The default environment was called default
  • =test It is not recommended to switch environments by modifying the default environment.

It is recommended to use activation method to activate the specified environment
It can also be activated using the command line.--=dev,hsqldb

You can also configure the default environment; components that do not label @Profile will always exist.

  • The default environment was called default
  • =test

It is recommended to use activation method to activate the specified environment

Environment includes

Notice:

and can only be used in files without profile, if written in it, it will be invalid.
You can also add an additional valid file instead of activating a replacement. for example:

[0]=common  #Contains the specified environment, regardless of which environment is activated[1]=local

Best Practical:

  • Effective environment = activated environment/default environment + included environment [the environment that is always to be effective]
  • This is what it is used in the project
    • Basic configuration mybatis, log, xxx: write to the included environment
    • DB and Redis that need to dynamically switch to change: write to the activated environment

Profile grouping

Create a prod group, specifying that contains db and mq configurations

[0]=db
[1]=mq #Activate the corresponding environment directly by group ,You can write environment configurations or other environment configuration groups in the group

Use --=prod, prod, db, mq configuration files will be activated

Profile configuration file

application-{profile}.properties can be used as a configuration file for a specified environment.
Activate this environment and the configuration will take effect. All configurations that are finally in effect are

  • : Main configuration file, effective at any time
  • application-{profile}.properties: Specify the environment configuration file, activate the specified environment to take effect

profile priority > application
All active configuration items of the project = Activate all items in the environment configuration + entries in the main configuration file that do not conflict with the environment configuration file

This is the end of this article about the implementation of springboot3 environment isolation. For more related springboot3 environment isolation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!