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.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][/html]

 [asp](http:>

相关文章
|
5月前
|
Java 数据库连接 数据库
Error:Internal error: (java.lang.NoClassDefFoundError) Could not initialize class com.intellij.util.
Error:Internal error: (java.lang.NoClassDefFoundError) Could not initialize class com.intellij.util.
|
6月前
|
网络安全
ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
|
Java Maven
【异常】java: Internal error in the mapping processor: java.lang.NullPointerException
【异常】java: Internal error in the mapping processor: java.lang.NullPointerException
298 0
|
6月前
|
Java 应用服务中间件 Apache
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
281 0
|
Nacos
解决:java.lang.IllegalStateException: Invalid host: lb://exam_farmer_service
解决:java.lang.IllegalStateException: Invalid host: lb://exam_farmer_service
388 0
解决:java.lang.IllegalStateException: Invalid host: lb://exam_farmer_service
|
Java
【异常】Failed to bind properties under ‘logging.level‘ to java.util.Map<java.lang.String, java.lang.Str
【异常】Failed to bind properties under ‘logging.level‘ to java.util.Map<java.lang.String, java.lang.Str
227 0
jdk动态代理:ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl
jdk动态代理:ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl
jdk动态代理:ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl
零基础学java---方法method(4)
零基础学java---方法method(4)
126 0
零基础学java---方法method(4)
零基础学java---方法method(3)
零基础学java---方法method(3)
95 0