$ Proxy25无法强制转换为我的类spring框架

时间:2011-04-18 11:48:38

标签: hibernate spring aop

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

4 个答案:

答案 0 :(得分:1)

最后我找到了溶剂:

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

                            

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

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

答案 1 :(得分:1)

如果您使用的是基于Java的配置,则需要在Configuration类上添加@EnableAspectJAutoProxy注释,并将proxyTargetClass设置为true。

@Configuration
@ImportResource("classpath:beanConfiguration.xml")
@ComponentScan(basePackages="com.bpjoshi.foo")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class NotificationConfig {

}

有关详细说明,请访问this official spring documentation

如果您使用spring的xml配置,请将以下代码添加到您的bean-config.xml

<aop:aspectj-autoproxy proxy-target-class="true"/>

答案 2 :(得分:0)

看起来,你正在使用Spring AOP Proxy并尝试将一些应该是UserDao的内容转换为UserDao,但由于代理内容,它不是UserDao。


有关详细信息,请发布导致错误的代码:

com.playence.app.daoTests.TestCreateOntologyDB.testGenerateGlobalAnnotation(TestCreateOntologyDB.java:49)

奇怪的是,您是否尝试过AbstractApplicationContext.getBean(String name, Class<T> requiredType)而不是显式演员?


以我的拙见:我强烈建议使用AspectJ

答案 3 :(得分:0)

您需要启用目标类代理才能正常工作。将属性proxyTargetClass设置为true以使其工作或创建PersonDao接口,将您的dao重命名为PersonDaoImpl并让您的类使用接口而不是类。