使用IDEA搭建maven+SSM(spring+springMVC+mybatis)项目时,出现Service注入出错问题

错误信息:org.springframework.beans.factory.BeanCreationException: creating bean with name ‘itemController’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.taotao.service.ItemServiceImpl com.taotao.controller.ItemController.itemServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.taotao.service.ItemServiceImpl] 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)

分析原因:
是由于在serivce层采用接口和实现类的形式。则在注入时,没有注入接口而是注入了其实现类,就会出现此错误。
因为我们通过实现类来继承的接口,然而,必须使用jdk提供的动态代理方法。即采用接口注入。
而不使用接口直接对实现类进行注入,则为cglib的注入。
所以既继承了接口又使用实现类来注入的方式,这样两种代理类都是没有办法使用的。

解决方法:
1.直接通过jdk去生成动态代理类,(原理要求必须实现接口
2.通过cglib去实现接口,直接使用代理类。(而不能实现接口)

猜你喜欢

转载自blog.csdn.net/qq_41459208/article/details/81699371