错误内容
java.lang.ClassCastException: com.sun.proxy.$Proxy44 cannot be cast to com.mm.service.imp.RoleServiceImp
测试代码
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
RoleServiceImp bean = (RoleServiceImp) context.getBean("roleService");
Role r = new Role();
r.setName("超级管理员");
bean.save(r);
其中继承关系,以及注入为:
@Service(value="roleService")
public class RoleServiceImp implements RoleService {...}
错误原因:通过context.getBean(“xxx”)获取到的对象要转为父接口类型
将代码改成如下即可:
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
RoleService bean = (RoleService) context.getBean("roleService");