java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to com

写spring切面方法运行后报错Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to com.wwn.spring.mylove.CalculatorImpl

错误显示类转换异常:

我的代码是写了一个Calculator接口定义了一个add方法,然后写了一个CalculatorImpl类实现该接口,写了一个AspectJ切面,然后在运行是报以上错误


这是因为spring aop在进行动态代理的结果是一个与CalculatorImpl同级的东西,可以理解为我写了一个接口A,然后写了一个实现类AB,然后我用spring aop为匹配的bean创建的代理可以理解为AC,AC也是实现了A的一个类,然后根据java语法规则,AB和AC之间是不能直接强转的,所以就报了以上类转换异常,解决方法就是直接写接口的类型。


猜你喜欢

转载自blog.csdn.net/irainreally/article/details/80524085