报错信息如下
Unsatisfied dependency expressed through field 'userManagerService';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'dubbo.exercise.one.basic.user.service.UserManagerService' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
在 Spring boot 的官网中有这样一段内容
We generally recommend that you locate your main application class in a root package above other classes. The @SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.
Spring Boot 项目在启动的时候是从启动类所在的包路径开始扫描相关类
接下来看看我们的项目结构


在这个项目中Spring Boot 的启动类 SpringBootStart 在dubbo.exercise.one.web路径下,根据官网中描述的内容我们可知,这个项目在启动的时候仅会扫描dubbo.exercise.one.web路径下的类,而在其它路径下的类不会被扫描到,故导致项目在启动的时候出现依赖服务注入失败的问题
对比图1和图2中的包路径可以发现其实整个项目的包路径我们都是以dubbo.exercise.one为前缀的
两种解决方式
- 指定扫描的包路径
@SpringBootApplication(scanBasePackages="dubbo.exercise.one")
public class SpringBootStart extends SpringBootServletInitializer {
// ...
}
- 将启动类
SpringBootStart.java调整到dubbo.exercise.one路径下

本文分析了Spring Boot多模块项目中因启动类位置导致的依赖服务找不到问题,指出项目启动时仅扫描启动类所在包及其子包的类。通过展示项目结构和Spring Boot官方建议,提出了解决方案:一是手动指定扫描包路径,二是调整启动类至合适位置。

2104

被折叠的 条评论
为什么被折叠?



