照書上步驟使用BeanNameAutoProxyCreator為項目添加了spring事務管理機制,可是在java代碼中使用StudentDAO stdao = (StudentDAO)ctx.getBean("StudentDAO");Student st = new Student("張三");stdao.save(st);報錯java.lang.ClassCastException: $Proxy0 cannot be cast to...網上說需要將ctx.getBean()方法的返回值用接口類接收,改為IStudentDAO stdao = (IStudentDAO)ctx.getBean("StudentDAO");Student st = new Student("張三");stdao.save(st);其中IStudentDAO是接口類,StudentDAO是它的實現類,這里為什么申明一個接口類可以直接使用實現類定義的方法呢?ctx.getBean()的返回值明明被強制轉換成了IStudentDAO啊,是不是因為Spring管理機制在搗鬼呢?希望有高手能詳細給我解釋解釋這樣用接口類的理由,如果是Spring事務管理在搗鬼,希望也能解釋一下為什么Spring事務管理要這么設計
- 1 回答
- 0 關注
- 1186 瀏覽
添加回答
舉報
0/150
提交
取消