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);
    }
}
posted @ 2021-12-07 22:03  奇风顿行  阅读(1436)  评论(0编辑  收藏  举报