Spring AOP,声明父母抛出异常 [英] Spring AOP, declare-parents cast exception

查看:33
本文介绍了Spring AOP,声明父母抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

  • 一个接口GenericDao
  • 一个 类 GenericDaoImpl 实现了 GenericDao
  • 一个类 UserDao
UserDao userDao;
public void setUserDao(UserDao val) { userDao = val; }
...
((GenericDao) userDao).update(user);

<小时>

我的 Beans.xml 看起来像:


My Beans.xml looks like:

<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 不能转换为 dao.GenericDao

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

I also tried setting default-impl to: dao.user.UserDao but result is the same.

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

I couldn't find any good documentation or examples showing proper xml configuration.

我的 XML 有什么问题?

What is wrong with my 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>)

推荐答案

我找到了解决方案.

  <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.

Needed to add plus sign (+) to types-matching and change default-impl to delegate-ref.

这篇关于Spring AOP,声明父母抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆