Spring Boot Learning 1.4 - Configuration Profile environment

Profile Spring file is different configuration functions to provide support for different environments, may be provided to activate, specify the parameters to quickly switch environment etc.
plurality 1. profile file name format: application- {profile} .properties:
for example two new documents: application -dev.properties, application-prod.properties. Are used to configure the information in the development and production environments, the default program is specified file or application.properties application.yml file.
You can write different server.port (port number) in the above document
such as we now need to use development environment, add this line of code in the default application.properties file: spring.profiles.active = dev, click Test main program, Here you can see the port number to call for the development of the port number configuration environment.
This is the port and activate the file application.properties environment:

The following is a port information after clicking the main program running, I set the port number in the file is application-dev.properties 8081

 Can also be configured in application.yml file we created earlier, before we have written a default port number is 8000, before the first activation of the properties file commenting out the code, you can use the "---" in yml file We are divided, as shown:

My side is activated dev environment, and above, after running our program calls are 8081 ports in the dev development environment.

In addition to setting environment activated in the configuration file, you can also use the tools provided by the idea

Enter the Program arguments to be --spring.profiles.active = dev, to activate the development environment, you can specify the following prod environment yml file, the port number is 8082, and then click Run spring boot, we can see the output column the port number is 8081 in the dev environment:

As an alternative, we can use the right of Maven tool, you need to add the following configuration information in the pom file:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

After the download is complete packet waiting IDEA clicks required package at right Maven packaged as operation: 

Then wait for the output field prompt to end, right-click on the left side of the target file show in explorer, type cmd in the input field above the path, the way to copy the file name of the just labeled jar package:

Then enter in cmd

java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

Click Enter will run the program will activate the dev environment is in port

Published 58 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37504771/article/details/95373977