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

简介: 1 严重: Servlet /N002-1.0 threw load() exception 2 java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.
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"/>

相关文章
|
6月前
|
Java Spring
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
30 0
|
3月前
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
51 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
6月前
|
Java 关系型数据库 MySQL
【Java异常】java.sql.SQLExcetion:Cannot convert value “0000-00-00 00:00:00” from column 9 to TIMESTAMP
【Java异常】java.sql.SQLExcetion:Cannot convert value “0000-00-00 00:00:00” from column 9 to TIMESTAMP
45 0
|
4月前
|
Java
【Java报错】java.lang.ClassCastException: xxxClass cannot be cast to java.lang.Comparable 问题重现+解决
【Java报错】java.lang.ClassCastException: xxxClass cannot be cast to java.lang.Comparable 问题重现+解决
42 0
|
5月前
|
SQL 关系型数据库 MySQL
mysql异常java.math.BigInteger cannot be cast to java.lang.Long
mysql异常java.math.BigInteger cannot be cast to java.lang.Long
|
6月前
|
XML 数据格式
解决 Cannot convert value of type ‘java.lang.String‘ to required type ‘java.sql.Driver‘ for property ‘
解决 Cannot convert value of type ‘java.lang.String‘ to required type ‘java.sql.Driver‘ for property ‘
118 0
|
6月前
|
Java
【Java异常】java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xxx‘ method
【Java异常】java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xxx‘ method
55 0
|
8月前
|
Java
java——成功解决cannot resolve symbol ‘xxx’
java——成功解决cannot resolve symbol ‘xxx’
|
9月前
|
应用服务中间件
Minio上传文件之后出现java.io.UncheckedIOException: Cannot delete D:/temp/tomcat问题的解决
Minio上传文件之后出现java.io.UncheckedIOException: Cannot delete D:/temp/tomcat问题的解决
373 0
|
9月前
|
关系型数据库 MySQL 数据库
【已解决】java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long异常,亲测有效
【已解决】java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long异常的正确解决方法,亲测有效!!!
287 0