$ Proxy25无法强制转换为我的类spring框架($Proxy25 cannot be cast to my class spring framework)

编程入门 行业动态 更新时间:2024-10-26 18:16:37
$ Proxy25无法强制转换为我的类spring框架($Proxy25 cannot be cast to my class spring framework)

我在运行测试时遇到此异常(我试图在spring中配置aop):

java.lang.ClassCastException: $Proxy25 cannot be cast to path.UserDao at com.playence.app.daoTests.TestCreateOntologyDB.testGenerateGlobalAnnotation(TestCreateOntologyDB.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59) at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98) at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79) at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42) at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

UserDao.java

public class UserDao extends AbstractHibernateDAOSupport { public UserDao() { super(); } /** * Insert a new User into the database. * * @param user */ public void store(User user) throws DataAccessLayerException { super.save(user); } /** * Delete a User from the database. * * @param user */ public void delete(User user) throws DataAccessLayerException { super.delete(user); } /** * Updates the state of a detached User. * * @param user */ public void update(User user) throws DataAccessLayerException { super.update(user); } public User findByID(String id) throws DataAccessLayerException { return (User) this.find(User.class, id); } /** * Finds all Users in the database. * * @return */ public List findAll() throws DataAccessLayerException { return super.findAll(User.class); }

Spring配置文件: applicationContext-dao.xml

<bean id="userDao" class="path.UserDao"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean>

TestCreateOntologyDB.java

.... ApplicationContext ctx ; public TestCreateOntologyDB() { ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); } @Test public void testGenerateGlobalAnnotation(){ UserDao userDao = (UserDao)ctx.getBean("userDao"); ...

我还没有在任何其他配置文件中设置任何UserDao附加属性。 可能是什么错误? 任何帮助,将不胜感激。 提前致谢

I am getting this exception while running a Test (I am trying to configure aop in spring):

java.lang.ClassCastException: $Proxy25 cannot be cast to path.UserDao at com.playence.app.daoTests.TestCreateOntologyDB.testGenerateGlobalAnnotation(TestCreateOntologyDB.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59) at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98) at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79) at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42) at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

UserDao.java

public class UserDao extends AbstractHibernateDAOSupport { public UserDao() { super(); } /** * Insert a new User into the database. * * @param user */ public void store(User user) throws DataAccessLayerException { super.save(user); } /** * Delete a User from the database. * * @param user */ public void delete(User user) throws DataAccessLayerException { super.delete(user); } /** * Updates the state of a detached User. * * @param user */ public void update(User user) throws DataAccessLayerException { super.update(user); } public User findByID(String id) throws DataAccessLayerException { return (User) this.find(User.class, id); } /** * Finds all Users in the database. * * @return */ public List findAll() throws DataAccessLayerException { return super.findAll(User.class); }

Spring configuration files: applicationContext-dao.xml

<bean id="userDao" class="path.UserDao"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean>

TestCreateOntologyDB.java

.... ApplicationContext ctx ; public TestCreateOntologyDB() { ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); } @Test public void testGenerateGlobalAnnotation(){ UserDao userDao = (UserDao)ctx.getBean("userDao"); ...

And I haven't set up any UserDao additional propperty in any other configuration file. What could be the error?? Any help would be appreciated. Thanks in advance

最满意答案

最后我找到了溶剂:

为将使用aop的类创建接口

在spring配置文件中编辑正确的路径:

在同一配置文件中添加此行。 请注意,在我的情况下,它将影响所有bean,但它可以只包含在一个bean中。

整个主题在http://forum.springsource.org/showthread.php?p=357883

Finally I found the sollution:

Create interface for the classes which will use aop

Edit the path to the correct one in spring configuration file:

Add this line in the same configuration file. Be aware that in my case it will influence in all the beans, but it can be included for just one bean.

The whole topic is here http://forum.springsource.org/showthread.php?p=357883

更多推荐

本文发布于:2023-08-07 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465930.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   框架   spring   class   cast

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!