1. 项目介绍
本项目包含一个父工程 demo 和 四 个子模块(demo-base, demo-dao, demo-service, demo-web), demo-base 为其他三个模块的公共内容, 四个模块都依赖父模块, demo-dao 依赖 demo-base; demo-service 依赖 demo-dao,也就间接依赖 demo-base; demo-web 依赖 demo-service,,也就间接依赖demo-base和demo-dao
2. 搭建思路
先创建一个 Spring Initializr 工程 demo 作为 父工程, 然后在父工程再建四个子 Module (demo-base, demo-demo-dao, demo-service),其实他们就是四个普通的Spring Initializr 工程
3. 开始搭建
首先,创建一个 Spring Initializr 项目和子 Module
!!!!注意: 修改demo 的 pom 文件中的打包方式为 pom
下面正式开始演示搭建多模块
(1) 第一步, 删除刚才创建工程里的文件, 只保留(.idea文件夹 , 和项目 pom 文件, 以及一个 *.iml 文件 )
2)第二步, 创建子 Module (demo-base, demo-dao, demo-service 和 demo-web) 先创建demo-base子工程
创建好了 demo-base 子项目 , 为子工程 demo-base 声明父工程 并 为父工程声明子 Module(在 demo 和 demo-base 的 pom 文件中添加如下代码)
在 demo-base 中 声明父工程,注意:此时demo-base继承的是 SpringBoot提供的父工程, 需要修改<parent></parent>中的版本信息, 修改成父项目 demo 的版本信息(直接将父项目 demo 的pom文件 中的版本信息复制粘贴到 mode-base中即可)
在子工程中声明父工程
<parent>
<groupId>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
在父工程中声明modle
<modules>
<module>demo-base</module>
</modules>
修改后的父工程 demo 的 pom 文件
<?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">
<modelVersion>4.0.0</modelVersion>
<!-- 父项目 demo 的版本信息 -->
<groupId>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<!-- 继承说明:这里继承SpringBoot提供的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 声明子模块 -->
<modules>
<module>demo-base</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改后的子模块 demo-base 的 pom 文件
<?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">
<modelVersion>4.0.0</modelVersion>
<!-- demo-base 的版本信息 -->
<groupId>demo</groupId>
<artifactId>demo-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-base</name>
<description>Demo project for Spring Boot</description>
<!-- 声明父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</plugin>
</plugins>
</build>
</project>
创建demo-dao, demo-service 创建方法一样, 这里只演示demo-dao的创建
创建demo-web 模块与demo-dao创建除了第4步, 完全相同
第三步 保留demo-web的启动类 和 配置文件, 其他项目的启动类都删除, 整个项目只需要一个启动类和一个配置文件
demo-base:
/demo/demo-base/src/main/java/demobase/demo/DemoBaseApplication.java 删除
/demo/demo-base/src/main/java/resource/* 删除
demo-dao:
/demo/demo-dao/src/main/java/demodao/demo/DemoDaoApplication.java 删除
/demo/demo-dao/src/main/java/resource/* 删除
demo-service:
/demo/demo-service/src/main/java/demoservice/demo/DemoServiceApplication.java 删除
/demo/demo-service/src/main/java/resource/* 删除
第四步
在 demo-dao 中添加 demo-base 的依赖信息,
<dependencies>
<!-- 添加 demo-base 的依赖 -->
<dependency>
<groupId>demo</groupId>
<artifactId>demo-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在 demo-service 中添加 demo-dao 的依赖信息
<dependencies>
<!-- 添加 demo-dao 的依赖 -->
<dependency>
<groupId>demo</groupId>
<artifactId>demo-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在 demo-web 中添加 demo-service 的依赖信息,
<dependencies>
<!-- 添加 demo-service 的依赖 -->
<dependency>
<groupId>demo</groupId>
<artifactId>demo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</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>
最后注意,demo-web留有我们唯一的启动类和配置文件。启动类只能扫描当前包下的所有文件加载为bean,所以,在demo-web的启动类上我们需要加上scanBasePackages="com.demo"
*com.demo* 是每个模块设置的包路径*
@SpringBootApplication(scanBasePackages="com.demo") public class BaseModelApplication {...}