本文介绍了Spring AOP,声明父母强制转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有:
- 接口GenericDao
- 类GenericDaoImpl实现GenericDao
- 类UserDao
- an interface GenericDao
- a class GenericDaoImpl implements GenericDao
- a class 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 cannot be cast to 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?
这里是完整的错误堆栈:
Here is full error stack:
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更改为委托引用.
Needed to add plus sign (+) to types-matching and change default-impl to delegate-ref.
这篇关于Spring AOP,声明父母强制转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!