springboot多模塊

經過幾天的踩坑,終於搭建了一套spring-boot屬於自己多模塊的項目。真是山重水複疑無路,柳暗花明又一村,終於衆裏尋他千百度,驀然回首,那人卻在,燈火闌珊處。好了,不嘚瑟了,咱們直入主題。

項目結構如下:

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的配置

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爲例進行講解 

在這裏注意所有模塊子包就將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>

做到此步驟,恭喜你搭建成功了。最後還是以一句成語“知易行難”來結束本次文章。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章