java入门017~springboot创建多Moudle多模块
我们在开发过程中,一个项目越来越庞大,再加上多个程序员同时开发一个项目,如果只有一个moudle的话,势必会造成很多代码冲突,所以今天来教大家如何实现一个多moudle(多模块)的springboot项目
本节知识点
1,springboot主项目的创建
2,springboot子项目的创建
3,springboot子项目间的相互调用
4,springboot多moudle项目的运行
一,创建主项目(父项目)
1,新建项目

2,创建springboot项目

3,包名要定义或者保持默认,项目名也可以自行修改,其余保持默认,然后点next下一步

4,使用springboot最新版本2.1.6,然后点击next

5,定义所在目录文件,点击finish

6,到这里父项目就创建成功了

7,可以把下面红框里5个不用的文件删除(也可以不用删)

二,创建子moudle(子项目)

创建子moudle和上面创建父项目步骤基本一致,有下面需要注意的点。
1,包名要和父项目保持一致。

2,子moudle要用到web依赖

3,子项目的目录一定要在父项目的目录里

4,创建好的子moudle如下

三,同样的方式,再创建一个moudle2

四,改造父pom.xml文件
1,父项目的pom.xml文件做如下改造,删除无用配置

2,设置打包为为pom

五,子项目pom.xml改造
moudle1和moudle2的 <packaging>都要设置为jar


六,把moudle2的入口application删除,只保留moudle1的即可。

删除后,项目结构如下

七,创建测试controller
在moudle1和moudle2里分别创建controller类

moudle1的Controller类代码如下
@RestController
public class Controller {
@GetMapping("/hello1")
public String hello1(){
return "我是moudle1";
}
}
moudle2的Controller2类代码如下
@RestController
public class Controller2 {
@GetMapping("/hello2")
public String hello1(){
return "我是moudle2";
}
}
八,启动项目

项目启动成功

访问hello1可以看到返回moudle1定义的信息

访问hello2缺报404错误

出现这个错误的原因,是因为moudle2里的controller2没有被注册进项目,所以我们需要添加如下代码。

@ComponentScan({"com.qcl"}) 就是扫描我们com.qcl包下的所有代码,并把这些代码注册到项目。加完这段代码,我们再重启项目。
就可以通过hello2成功访问到moudle2的信息了。

到这里我们就成功的创建了多moudle的springboot项目了。
还有一点,需要在父项目的pom.xml把我们的两个子模块配置进去,代码如下

下面我把父项目的子项目的pom.xml文件完整的贴出来给大家。
1,父项目pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qcl</groupId>
<artifactId>moudles</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>moudles</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<modules>
<module>moudle1</module>
<module>moudle2</module>
</modules>
</project>
2,子项目moudle1的pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qcl</groupId>
<artifactId>moudle1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>moudle1</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!-- 在moudle1中引入moudle2依赖 -->
<dependency>
<groupId>com.qcl</groupId>
<artifactId>moudle2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3,子项目moudle2的pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qcl</groupId>
<artifactId>moudle2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>moudle2</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
源码:
https://github.com/qiushi123/moudles
视频讲解
https://edu.csdn.net/course/detail/23443