org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userServiceImpl' is expected to be of type 'com.bjpowernode.crm.settings.service.impl.UserServiceImpl' but was actually of type 'com.sun.proxy.$Proxy17'
出现BeanNotOfRequiredTypeException这个异常的原因:
使用事务管理service实现类,在controller层自动注入service对象时,使用的是实现类,会出现BeanNotOfRequiredTypeException对象类 型不匹配异常
错误使用:
@Controller
public class UserController{
@Autowired
private UserServiceImpl service;
}
修改后:
@Controller
public class UserController{
@Autowired
private UserService service;
}
总结:
第一种情况(jdk动态代理):
- service层有接口,有实现类
- 事务管理service实现类方法,
- 在controller层定义service作为属性使用接口对象:
@Autowired
private UserService service; //UserService这是接口
第二种情况(cglib动态代理):
- service层无接口,只有service类
- 事务管理service类的方法,
- 在controller层定义service作为属性使用接口对象:
@Autowired
private UserService service; //UserService这是类