SSH整合 ClassCastException

添加事務後,測試出現問題:


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方法。

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