【Bug】Spring Boot 多模块开发 找不到依赖服务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/quan20111992/article/details/88616828

报错信息如下

 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 项目在启动的时候是从启动类所在的包路径开始扫描相关类

接下来看看我们的项目结构
在这里插入图片描述

图1

在这里插入图片描述

图2

在这个项目中Spring Boot 的启动类 SpringBootStartdubbo.exercise.one.web路径下,根据官网中描述的内容我们可知,这个项目在启动的时候仅会扫描dubbo.exercise.one.web路径下的类,而在其它路径下的类不会被扫描到,故导致项目在启动的时候出现依赖服务注入失败的问题

对比图1和图2中的包路径可以发现其实整个项目的包路径我们都是以dubbo.exercise.one为前缀的

两种解决方式

  1. 指定扫描的包路径
@SpringBootApplication(scanBasePackages="dubbo.exercise.one")
public class SpringBootStart extends SpringBootServletInitializer {
	// ...
}
  1. 将启动类SpringBootStart.java调整到dubbo.exercise.one路径下
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/quan20111992/article/details/88616828
今日推荐