Spring AOP, declare-parents 转换异常
Spring AOP, declare-parents cast exception
我有:
- 一个接口GenericDao
- a class GenericDaoImpl 实现 GenericDao
- a class 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$7148da.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。
我有:
- 一个接口GenericDao
- a class GenericDaoImpl 实现 GenericDao
- a class 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$7148da.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。