一、問題背景
SpringBoot拆分為多模塊項目(按不同形式拆分)
按層次拆分:controller一個項目、service一個項目、dao(mapper)一個項目
按業務拆分:一個業務模塊拆成一個項目(包含該業務的controller、service、dao等)
拆分完成,啟動原來的Application后,提示 APPLICATION FAILED TO START
二、問題現象
Description:
Field userMapper in com.fanzyx.crane.framework.security.service.JwtUserDetailsService required a bean of type 'com.fanzyx.xx.mapper.XxxrMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.fanzyx.xx.mapper.XxxrMapper' in your configuration.
或者提示找不到Mybatis-plus 的BaseMapper中的某個方法(你調用的方法)
三、問題原因
先排查下包引用是否沖突,如果沒沖突再看下面
Application中沒有配置mapper掃描的基礎包路徑,就只會在當前模塊下尋找
e.g.
@SpringBootApplication()
public class XxxApplication {
public static void main(String[] args) {
SpringApplication.run(XxxApplication.class,args);
}
}
四、解決方案
在XxxApplication(啟動類) 上增加注解並加入掃描基礎路徑
// 多模塊項目需要配置掃描基礎包,否則會無法裝載其他模塊的類
@SpringBootApplication(scanBasePackages = {"com.fanzyx.xx"})
// 一定要配置到具體路徑,中間 ** 可以匹配多層級目錄,一個 * 只能匹配單層級目錄
//@MapperScan(basePackages = {"com.fanzyx.xx.**.mapper"})
public class XxxApplication{
public static void main(String[] args) {
SpringApplication.run(XxxApplication.class,args);
}
}