解决:Injection of autowired dependencies failed报错问题
解决如下报错:
Error creating bean with name ‘categoryManageController’: Injection of
autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.juhuasuan.service.IUserService
com.juhuasuan.controller.backend.CategoryManageController.iUserService;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘iUserService’: Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.juhuasuan.dao.UserMapper
com.juhuasuan.service.Impl.UserServiceImpl.userMapper; nested
exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name ‘userMapper’ defined in file
[E:\neusoft\java\maven\maven_code\juhuasuan\target\classes\com\juhuasuan\dao\UserMapper.class]:
Unsatisfied dependency expressed through bean property
‘sqlSessionFactory’: : Error creating bean with name
‘sqlSessionFactory’ defined in class path resource
[applicationContext-datasource.xml]: Invocation of init method failed;
nested exception is org.springframework.core.NestedIOException: Failed
to parse mapping resource: ‘file
[E:\neusoft\java\maven\maven_code\juhuasuan\target\classes\mappers\CartMapper.xml]’;
nested exception is org.apache.ibatis.builder.BuilderException: Error
parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException:
Error resolving class. Cause: org.apache.ibatis.type.TypeException:
Could not resolve type alias ‘BaseResultMap’. Cause:
java.lang.ClassNotFoundException: Cannot find class: BaseResultMap;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘sqlSessionFactory’ defined in class path
resource [applicationContext-datasource.xml]: Invocation of init
method failed; nested exception is
org.springframework.core.NestedIOException:
遇到情况如下:
1.相关的bean已注册
2.所有的注解(@Controller,@Service,@Autowired)该加的地方都加上了
3.问题:自动装配的依赖注入失败,Injection of autowired dependencies failed
报错信息中的关键问题:(看报错信息要看全,要看到最后)
下面就是我在一堆报错信息中找到的核心问题:
[E:\neusoft\java\maven\maven_code\juhuasuan\target\classes\mappers\CartMapper.xml]’;
Could not resolve type alias ‘BaseResultMap’. Cause:
java.lang.ClassNotFoundException: Cannot find class: BaseResultMap;
分析:在CartMapping.xml文件中无法解析别名为BaseResultMap,并且找不到该类,也就是说写了该类的地方出错,这里我是将结果集中resultMap写成resultType
错误的写法:
正确的写法:由于该方法返回结果集的是map,所以用的是resultMap
最后说说我对这个报错的理解:
自动装配的依赖注入失败
我是这样理解的,首先service依赖注入的是mapper文件,mapper文件的注解是@Autowired,所以mapper出错时,Autowired这个注解自然就不能自动装配到mapper文件中,所以就会出现报错Injection of autowired dependencies failed