gpt4 book ai didi

hibernate - $Proxy25 无法转换为我的类 Spring 框架

转载 作者:行者123 更新时间:2023-12-03 00:36:41 25 4
gpt4 key购买 nike

我在运行测试时遇到此异常(我正在尝试在 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 附加属性。可能是什么错误?任何帮助,将不胜感激。提前致谢

最佳答案

最后我找到了解决方案:

  1. 为将使用aop的类创建接口(interface)
  2. 在 spring 配置文件中编辑正确的路径:

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

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

关于hibernate - $Proxy25 无法转换为我的类 Spring 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5702364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com