首页 / JAVA / java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService
java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2171字,纯文字阅读大概需要4分钟。
内容图文
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动态代理。所有该目标类型实现的接口都将被代理。例如,我自己编写的ServiceImpl类实现了Service接口,就在使用getBean()时,就不能使用ServiceImpl去接收,而要使用Service这个接口去接收
SalaryService salaryservice =(SalaryService) new ClassPathXmlApplicationContext("Bean.xml").getBean("salaryservice");
若该目标对象没有实现任何接口,则创建一个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"/>
原文:http://www.cnblogs.com/zqr99/p/7745761.html
内容总结
以上是互联网集市为您收集整理的java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService全部内容,希望文章能够帮你解决java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。