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方式,引入各个模块的配置文件
不积跬步无以至千里
原文地址:https://www.cnblogs.com/xiaogblog/p/14366000.html