美文网首页
SpringBoot多模块

SpringBoot多模块

作者: 刘荣杰 | 来源:发表于2021-05-16 14:01 被阅读0次

为什么要有多模块

单模块开发所面临的问题:
1.不同方面代码耦合,系统出现问题很难定位,可能在修正问题出现更多问题。
2.新入开发者介入成本高。
3.代码边界很模糊,多人合作不好配合。

所以出现了以maven管理的多模块应用,每个模块对应着一个pom.xml,之间通过继承和聚合相互关联。
多模块优点:
1.降低代码耦合度。
2.每个模块可以自解释,即可以只启动相应的模块。
3.多人协同边界定义清晰。
4.重点模块甚至可以抽象出来作为公共服务。

Spring中多模块配置

1.创建父工程,创建子模块


image.png

2.编辑父工程pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <!--parent指明继承关系,给出被继承的父项目的具体信息-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo5</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo5</name>
    <description>Demo project for Spring Boot</description>
    
    <!--父模块打包类型必须为pom-->
    <packaging>pom</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <!--模块声明:这里有很多子模块-->
    <modules>
        <module>model</module>
        <module>web</module>
        <module>server</module>
    </modules>
    
    <!--版本说明:这里统一管理依赖的版本-->
    <dependencyManagement>
        <dependencies>
            <!--module-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>server</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>model</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>

        </dependencies>
    </dependencyManagement>
</project>

4.编辑子模块pom
model模块:运行数据库模型

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>model</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo5</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>
                        src/main/resources/mybatis-generator-config.xml
                    </configurationFile>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.48</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>


</project>

server模块:核心服务,依赖于model

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo5</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>model</artifactId>
        </dependency>
    </dependencies>



</project>


web模块:对外提供api查询接口,依赖于server

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo5</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

相关文章

网友评论

      本文标题:SpringBoot多模块

      本文链接:https://www.haomeiwen.com/subject/mggqjltx.html