【问题标题】:Spring AOP, declare-parents cast exceptionSpring AOP,declare-parents 强制转换异常
【发布时间】:2015-12-13 10:22:22
【问题描述】:

我有:

  • 一个接口GenericDao
  • 一个类 GenericDaoImpl 实现了 GenericDao
  • 一个类UserDao

我想做的是:

UserDao userDao;
public void setUserDao(UserDao val) { userDao = val; }
...
((GenericDao) userDao).update(user);

我的 Beans.xml 看起来像:

<bean id="genericUserDao" class="dao.GenericDaoImpl">
  ...
  <property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>

<bean id="userDao" class="dao.user.UserDao">
  <property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>

<aop:config>
  <aop:aspect>
    <aop:declare-parents
            types-matching="dao.user.UserDao"
            implement-interface="dao.GenericDao"
            default-impl="genericUserDao" />
  </aop:aspect>
</aop:config>

这是导致

java.lang.ClassCastException: dao.user.UserDao cannot be cast to dao.GenericDao

我也尝试将 default-impl 设置为:dao.user.UserDao 但结果是一样的。

我找不到任何显示正确 xml 配置的好的文档或示例。

我的 XML 有什么问题?

编辑:

这是完整的错误堆栈:

Exception in thread "Thread-3" java.lang.ClassCastException: dao.user.UserDao cannot be cast to dao.GenericDao
    at dao.GenericDao$$FastClassBySpringCGLIB$$d43da5ef.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at dao.user.UserDao$$EnhancerBySpringCGLIB$$957148da.update(<generated>)

【问题讨论】:

    标签: java spring javabeans aop mixins


    【解决方案1】:

    我找到了解决办法。

      <aop:config>
        <aop:aspect>
          <aop:declare-parents
                  types-matching="dao.user.UserDao+"
                  implement-interface="dao.GenericDao"
                  delegate-ref="genericUserDao" />
        </aop:aspect>
      </aop:config>
    

    需要在类型匹配中添加加号 (+) 并将 default-impl 更改为 delegate-ref。

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      相关资源
      最近更新 更多