SSH整合 ClassCastException

来源:互联网 发布:java多线程socket通信 编辑:程序博客网 时间:2024/06/01 16:38

添加事务后,测试出现问题:


java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to itcast.test.service.impl.TestServiceImpl

at itcast.test.TestMerge.testAdd(TestMerge.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
.......


测试代码:

@Test
public void testAdd() {
TestServiceImpl testServiceImpl = (itcast.test.service.impl.TestServiceImpl) ac.getBean("testService");

testServiceImpl.save(new Person("测试四"));
}


更改代码为:

@Test
public void testAdd() {
TestService testService =  (TestService) ac.getBean("testService");

testService.save(new Person("测试五"));
}

解决问题.


病因: 
对于Spring AOP 采用两种代理方法,一种是常规JDK,一种是CGLIB,我的UserDao了一个接口IUserDao,当代理对象实现了至少一个接口时,默认使用JDK动态创建代理对象,当代理对象没有实现任何接口时,就会使用CGLIB方法。

0 0
原创粉丝点击