springboot study notes (seven)

Multi-environment configuration and switching

Several environments are involved in the development process of a project, such as development environment, test environment, implementation environment, etc.

So how to switch between these environments?

1. Configure via application.properties

By default springboot will read the environment configuration in application.properties

But = But if there are multiple environments, you need to rewrite the configuration file.

Naming method: application-environment name.properties

application-dev.properties

application-test.properties

If you want to select a specific environment, you need to specify it in the application.properties file: spring.profiles.active=environment name

If you only comment out the configuration in the application.properties file, springboot will still read other environment configuration files application-environment name.properties .

The priority of properties is higher than yml.

Example:

Two other environment configuration files are newly created under the resource folder:

Specify the port number separately (here, we only take the port number as an example):

 

Specify the operating environment in the application.properties file:

 run:

2. Configure via application.yml

Example:

Configure three environments in application.yml:

Specify in the main environment:

test:

 Three, dynamic switching environment

         1. Specified by operating parameters

                          a)STS(Eclipse):右键-》Run Configuration -》Argument -》program Argument

                                   --spring.profiles.active=Environment name

 

test: 

                          b) Command line project-"right click-"run as-" maven build...

                                   First type the maven project into a jar package, and then run it on the command line

                                   java -jar project name.jar --spring.profiles.active=environment name

         2. Specified by the vm parameter

-Dspring.profiles.active=Environment name

 test:

 

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/82955971