SpringBoot+Maven多模塊項目,啟動后提示找不到XxxService


一、問題背景

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);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM