异常是这样的
Error creating bean with name 'searchItemServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.e3mall.search.mapper.ItemMapper cn.e3mall.search.service.impl.SearchItemServiceImpl.itemMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.e3mall.search.mapper.ItemMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Spring注入失败,我的第一反应也是没有打@Service注解,或者是@Service的包导错了(因为alibaba 也有个这样的包)。
但是经过检查并没有问题。碰到这类型的错误真的是蛋疼,浪费了不少时间。
最后一步步排查发现是这个类中间的定义的一个成员变量注入错误,并不是网上说的 @Service @AutoWired的都有问题。
原因是: Mybatis框架配置文件中的扫描的路径错误(配置文件配置的Bean),导致Spring不能注入到那个成员变量里面去。我这么表达也不是很清楚,不过总结一句:
碰到注入失败的错误,如果不是没有打注解,那有可能是配置文件的扫描位置写错了,不一定只在applicationXXX.xml文件里面,还可能是其他的配置文件里写错了。多找找吧。好麻烦。