【Bug】Spring Boot 多模块开发 找不到依赖服务
报错信息如下
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
路径下