Spring AOP,declare-parents施放异常

时间:2015-09-16 17:41:06

标签: java spring javabeans aop mixins

我有:

  • 界面GenericDao
  • 类GenericDaoImpl实现GenericDao
  • 类UserDao

我想做的是:

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

My 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>)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

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