java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.Question...

 1 严重: Servlet /N002-1.0 threw load() exception
 2 java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService
 3     at com.bbk.n002.servlet.CreateTaskQueueServlet.init(CreateTaskQueueServlet.java:28)
 4     at javax.servlet.GenericServlet.init(GenericServlet.java:160)
 5     at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
 6     at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
 7     at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
 8     at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
 9     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
10     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
11     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
12     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
13     at java.util.concurrent.FutureTask.run(Unknown Source)
14     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
15     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
16     at java.lang.Thread.run(Unknown Source)

Spring的文档中这么写的:Spring AOP部分使用JDK动态代理或者CGLIB来为目标对象创建代理。如果被代理的目标实现了至少一个接口,则会使用JDK动态代理。所有该目标类型实现的接口都将被代理。若该目标对象没有实现任何接口,则创建一个CGLIB代理。

        所以,解决办法是,如果用JDK动态代理,就必须为被代理的目标实现一个接口(要注意的地方是:需要将ctx.getBean()方法的返回值用接口类型接收);如果使用CGLIB强制代理,就必选事先将CGLIB包导入项目,设置beanNameAutoProxyCreator的proxyTargetClass属性为true。

这里DeviceService实现了接口IDeviceService,所以会使用JDK动态代理,从而使(DeviceService) deviceService强制转换的时候报错(因为实现IDeviceService接口的代理类是属于DeviceService子类)。因此需要使用CGLIB强制代理。需要在springContext.xml中增加如下配置:


[html]
<aop:aspectj-autoproxy proxy-target-class="true"/> 

<aop:aspectj-autoproxy proxy-target-class="true"/>

转载于:https://www.cnblogs.com/liuling/p/2014-8-23-001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值