springboot多模块

经过几天的踩坑,终于搭建了一套spring-boot属于自己多模块的项目。真是山重水复疑无路,柳暗花明又一村,终于众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。好了,不嘚瑟了,咱们直入主题。

项目结构如下:

springboot多模块

common:公共模块 一些工具类(公共模块),和对一些项目的异常进行封装。

main:是springboot项目启动类(启动模块),和一些项目配置文件的封装。

test:测试的模块(普通模块)

user:用户模块(普通模块)

搭建步骤:

一、首先我们配置xm下的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 项目说明:这里作为聚合工程的父工程 -->
    <groupId>com.xm</groupId>
    <artifactId>xm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 模块说明:这里声明多个子模块 -->
    <modules>
        <module>main</module>
        <module>user</module>
        <module>test</module>
        <module>common</module>
    </modules>




    <!-- 继承说明:这里继承SpringBoot提供的父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>

    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.test.skip>true</maven.test.skip>
        <maven.test.failure.ignore>true</maven.test.failure.ignore>
        <!-- 改变thymeleaf检查html的松紧度,使标签不闭合成为合法 start -->
        <!--<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>-->
        <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
        <!-- 改变thymeleaf检查html的松紧度,使标签不闭合成为合法 end -->
    </properties>



    <dependencyManagement>
        <!-- 模块说明:这里声明多个子模块 -->
        <dependencies>

            <!--子版本依赖-->
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



<!---->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>



    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>

</project>

 

1、这里的pom文件是父的pom文件,所有模块我们称作为该文件的子模块 

2、在这里我们注意三点: 

    1、将该文件打包的属性改成<packaging>pom</packaging>

    2、将所有子模块进行引入 

 <modules>
    <module>main</module>
    <module>user</module>
    <module>test</module>
    <module>common</module>
 </modules>

    3、将所有子模块用到的公共的包提出来,将来直接继承给子模块。在这里就将springboot一些依赖提了出来。

二、下面介绍关于main的配置

springboot多模块

main的主要功能是启动springboot项目,配置公共的配置文件,这里需要注意的是将来其他模块的包名必须相同且是该包的子包,这样做的目的是springboot启动文件能扫描到子模块下的包和配置文件扫描同样的包。在这里一定注意,本人在这里踩过坑。

       

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.xm</groupId>
        <artifactId>xm</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>main</artifactId>
    <packaging>war</packaging>

    <dependencies>


        <dependency>
            <groupId>com.xm</groupId>
            <artifactId>user</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.xm</groupId>
            <artifactId>test</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>

注意:

1、在这里将该模块打包成war包 

2、导入父级模块 

<parent>
    <groupId>com.xm</groupId>
    <artifactId>xm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

3、将所有的普通模块导入进来(一定要全部导入,在这里也踩过坑。)

<dependency>
    <groupId>com.xm</groupId>
    <artifactId>user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

<dependency>
    <groupId>com.xm</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

三、普通模块(user,test)

在这里我们以test为例进行讲解 

springboot多模块

在这里注意所有模块子包就将com.xm.sb,这里也是抗,一定要相同,否则扫描不到。但是controller的名字不好相同,否则会报错。

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.xm</groupId>
        <artifactId>xm</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>test</artifactId>

    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <dependency>
            <groupId>com.xm</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>

做到此步骤,恭喜你搭建成功了。最后还是以一句成语“知易行难”来结束本次文章。