引言
在这里我会带着实现一下SpringBoot的父子多模块项目
多模块项目
适用于一些比较大的项目中,通过模块拆分有以下几点好处
- 代码复用
- 防止POM变得过于庞大
- 方便二次开发
- 方便解耦
总结:方便
开始创建
一、创建父模块
1.项目结构
2.创建springboot项目
创建即可
3. 删除多余文件
剩下这些即可,其他删掉
二、创建子模块
1.创建springboot项目
加入子模块-> 右键父模块->新建->模块
1.1创建admin、service、common模块
1.1.1正常创建SpringBoot项目
【创建就完事了,先不要去动其他的】
1.1.2 删除多余文件,实现以下结构
父模块结构图
子模块结构图
除了admin有启动类,其他都要删掉启动类和test
普通模块:service、common
接口模块:admin
三、修改父子pom文件
1.修改父pom
dependencyManagement:统一管理依赖版本
2. 修改子模块admin的pom
3. 修改子模块common的pom
4. 修改子模块service的pom
依赖根据需求来添加即可
parent标签用于继承父模块
四、测试
接口模块admin调用模块service下的逻辑service
【代码】
controller
package com.smart.controller;
import com.smart.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/hello")
public String hello() {
return testService.res();
}
}
service
package com.smart.service;
import org.springframework.stereotype.Service;
@Service
public class TestService {
public String res(){
return "成功";
}
}
【结果】
五、常见问题
1、出现模块没有被扫描到bean
解决:你应该是用了maven创建的,改为SpringBoot创建即可