IDEA SpringBoot 模塊化構建


IDEA SpringBoot 模塊化構建

為什么使用模塊化構建?根本原因:解耦

創建父工程

新建父項目

  • idea 中選擇 File / New / Project / Spring Initializr
  • News Project 彈窗中 Type 選擇 Maven POM
  • 此時新建的 springboot 項目中只有一個 pom.xml 文件

修改 pom.xml 文件

  • 將一些用於項目規范性質或全局性質的 dependency 放在 dependencies 內
  • 將其它的 dependency 全部放在 dependencyManagement 內
  • 在 properties 內定義 dependency 版本
<?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>

    <packaging>pom</packaging>

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

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <mybatis-plus.version>3.1.2</mybatis-plus.version>
    </properties>

    <modules>
        <module>son-first</module>
        <module>son-second</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

創建子模塊

新建 module 子模塊

  • 項目名上右擊選擇 New / Module / Spring Initializr
  • News Project 彈窗中 Type 選擇 Maven Project
  • 此時新建的 springboot module 中包含完整結構 src / main|test / java|resources
  • 子模塊分兩種,一種是打包成為可運行的jar/war,一種是作為一方庫/項目依賴供給其它模塊使用

若是子模塊作為可運行jar,則子模塊的pom.xml文件需要添加maven插件用來打包,若是作為一方庫則不需要插件並刪除啟動類,其它部分相同

<?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>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>son-first</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork> <!-- 如果沒有該配置,devtools不會生效 -->
                    <!-- 指定該Main Class為全局的唯一入口 -->
                    <mainClass>子模塊啟動類完整類名</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

注意:

  • 由於SpringBoot默認掃描自身模塊的組件,所以依賴其它的模塊時,無法找到對應Bean,
    需要在啟動類上添加 @ComponentScan(basePackages = "com.example") 注解,basePackages指定到groupId即可
  • 在使用mybatis等ORM框架時,若將DAO層單獨作為module,在dao接口上使用@Mapper注解也會出現找不到mapper的情況
    ,其原因和上面相同,需要在啟動類或JavaConfig上添加 @MapperScan("dao層完整路徑"),推薦在DAO模塊中config目錄下添加
    MybatisConfig類,在類上添加 @MapperScan 注解
  • mybatis使用XML寫SQL時,也要注意掃描xml文件位置。將SQL XML文件放在DAO模塊中resources目錄下新建的mapper文件夾里,
    則需要在yml/properties文件中添加 mybatis.mapper-locations=classpath*:mapper/*.xml,classpath*中*代表掃描所有module中的resources目錄,
    mapper/*.xml中的*代表resources目錄中mapper文件夾里的所有xml文件
  • 即使每個模塊都有自己的yml/properties文件,但作為依賴后,只有主工程的yml/properties起作用,可以使用spring.profiles.include方式,引入各個模塊的配置文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM