SpringMVC学习笔记-Context initialization failed错误

Context initialization failed- ->上下文初始化错误

今天写项目时遇到一个问题
在这里插入图片描述
看了下日志.报错了很多这种↓

Error creating bean with name ‘systemServiceImpl’: Unsatisfied dependency expressed through field ‘systemMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘newer.mvc.mapper.SystemMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

太长可以不看 意思就是我的Service实现类无法通过@Autowired自动装载
是因为Service里面mybatis的映射类mapper的@Autowired也无法自动装载
看来看去不会报错 于是到处查资料 最后找到以下结论

因为 @Mapper 这个注解是 Mybatis 提供的,而 @Autowried 注解是 Spring 提供的,IDEA能理解 Spring 的上下文,但是却和 Mybatis 关联不上。而且我们可以根据 @Autowried 源码看到,默认情况下,@Autowried 要求依赖对象必须存在。

原帖
我的理解是:Spring的@Autowried要求对象的依赖实例必须存在 在运行时会去找寻相应的bean 找不到就直接报错了(这个时候mybatis还没有提供mapper的实现)所以会报错

解决办法

我给Service中的mapper的@Autowried注解设置为required=false
设置以后@Autowried就不会去校验了
在这里插入图片描述
不对的地方请执教.

猜你喜欢

转载自blog.csdn.net/qq_36008278/article/details/113795320
今日推荐