一、問題背景
SpringBoot拆分為多模塊項目(按不同形式拆分)
按層次拆分:controller一個項目、service一個項目、dao(mapper)一個項目
按業務拆分:一個業務模塊拆成一個項目(包含該業務的controller、service、dao等)
拆分完成,啟動原來的Application后,提示 APPLICATION FAILED TO START
二、問題現象
Description:
Field deptService in com.fanzyx.xx.controller.XxxController required a bean of type 'com.fanzyx.xx.service.XxxService' 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.service.XxxService' in your configuration.
三、問題原因
Application中沒有配置掃描的基礎包路徑,就只會在當前模塊下尋找
e.g.
@SpringBootApplication()
public class XxxApplication {
public static void main(String[] args) {
SpringApplication.run(XxxApplication.class,args);
}
}
四、解決方案
在@SpringBootApplication()注解中加入掃描基礎路徑
// 多模塊項目需要配置掃描基礎包,否則會無法裝載其他模塊的實體
@SpringBootApplication(scanBasePackages = {"com.fanzyx.crane"})
public class XxxApplication {
public static void main(String[] args) {
SpringApplication.run(XxxApplication .class,args);
}
}