IDEA's profile combined with Maven build different development environments (SpringBoot)

I. Overview

  During development, our project will have different development environments, such as the development environment, production environment, test environment, and our project some configuration is not the same in different environments, such as data source configuration, log files, configuration, etc. when if every time we deploy software to a different environment, we need to modify the appropriate configuration files, modify the to and fro, it is less prone to change places, and waste our workforce. Maven project with the profile to distinguish between different environment configuration, I have just come to learn about.

Second, the data preparation

  Data Preparation: Prepare three databases, database called test_db, table name for the student, but different data.

(1)192.168.229.134

 

 

(2)192.168.229.133

 

 

(3)192.168.229.132

 

 

Third, the application demo

  In this presentation, IDEA binding profile maven used to implement the switching of the switching and database application port. Engineering structure is shown:

 

 

profile configuration (1) the parent pom file

 <! - configure different profile, corresponding to different production environment ->
    <profiles>
        <profile>
            <! - Development ->
            <id>dev</id>
            <activation>
                <! - default development environment ->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
        </profile>
        <profile>
            <!--生产-->
            <id>pro</id>
            <properties>
                <activatedProperties>pro</activatedProperties>
            </properties>
        </profile>
        <profile>
            <!--生产-->
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>
    </profiles>

(2)application.properties的配置

spring.profiles.active=@activatedProperties@  //这里名称与profile中的标签名一致

(3)application-xxx.properties的配置(3个基本一致,数据库地址和应用上下文根不同)

 

 

(4)测试

①默认情况:父pom中已指定默认情况为开发环境,连接192.168.229.132数据,学生名应该为王五,上下文根为/dev-app,启动springboot测试如下:

 

 打开浏览器访问:localhost/dev-app/all?base=1

 

 

②指定测试环境,编辑启动配置添加:-Dspring.profiles.active=test

 

 启动程序,查看控制台日志如下

 

 打开浏览器访问:http://localhost/test-app/all?base=1,连接192.168.229.134数据,学生名应该为张三

 

 

③指定生产环境:-Dspring.profiles.active=test,控制日志如下:

 

 浏览器访问:http://localhost/pro-app/all?base=1

 

 

 

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11939397.html