service层实现类serviceImpl单元测试,报错org.springframework.beans.factory.UnsatisfiedDependencyException,报代理错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.microboy.demo.service.Impl.AreaServiceImplTest': Unsatisfied dependency expressed through field 'areaServiceImpl'; 


nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'areaServiceImpl' is expected to be of type 'com.microboy.demo.service.Impl.AreaServiceImpl' but was actually of type 'com.sun.proxy.$Proxy75'

 

这个错误的根本原因看最后,在使用annotation(注解)的方式注入spring的bean 时,spring采用代理的机制导致的

    方案一:由于AreaServiceImpl 是一个实现类,使用JDK的动态代理不能注入,jdk的动态代理不支持类的注入,只支持接口方式注入,如果确时不想用spring提供的动态代理,则此时可以选择用cglib代理解决。必须引入cglib的外部包

   方案二:JDK的动态代理需要的是接口方式注入,那就将上面的注入类型修改为接口如下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章