maven分层开发后遇到controller不能注入service层 org.springframework.beans.factory.UnsatisfiedDependencyException:

在service层中使用注解测试没有问题
但是在controller层不能注入
在这里插入图片描述
在这里插入图片描述

11-Sep-2020 13:25:49.601 严重 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed
 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'itemsController': 
Unsatisfied dependency expressed through field 'itemsService';
 nested exception is 

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.itheima.ssm.service.ItemsService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

controller中的配置文件
在这里插入图片描述

解决方法:
在controller中的配置文件
组件扫描调大一级后就能运行成功
在这里插入图片描述
在这里插入图片描述

原因分析:

分层开发后:maven会把controller相互依赖得层打成jar包放入到controller中

在这里插入图片描述
在这里插入图片描述
所以自我分析原因是:
把相互依赖得层打成jar包后controller得组件扫描不能扫描到spring容器中得service层,所以把controller调大一级后能注入service

猜你喜欢

转载自blog.csdn.net/weixin_49723367/article/details/108532992
今日推荐