Java.lang.ClassCastException:com.sun.proxy. $Proxy cannot is cast to Com.bbk.n002.service.QuestionService

Source: Internet
Author: User

1Severity: servlet/n002-1.0threw load () exception2 Java.lang.ClassCastException:com.sun.proxy. $Proxy cannot is cast to Com.bbk.n002.service.QuestionService3At Com.bbk.n002.servlet.CreateTaskQueueServlet.init (createtaskqueueservlet.java:28)4At Javax.servlet.GenericServlet.init (genericservlet.java:160)5At Org.apache.catalina.core.StandardWrapper.initServlet (standardwrapper.java:1280)6At Org.apache.catalina.core.StandardWrapper.loadServlet (standardwrapper.java:1193)7At Org.apache.catalina.core.StandardWrapper.load (standardwrapper.java:1088)8At Org.apache.catalina.core.StandardContext.loadOnStartup (standardcontext.java:5176)9At Org.apache.catalina.core.StandardContext.startInternal (standardcontext.java:5460)TenAt Org.apache.catalina.util.LifecycleBase.start (lifecyclebase.java:150) OneAt Org.apache.catalina.core.containerbase$startchild.call (containerbase.java:1559) AAt Org.apache.catalina.core.containerbase$startchild.call (containerbase.java:1549) - At Java.util.concurrent.FutureTask.run (Unknown Source) - At Java.util.concurrent.ThreadPoolExecutor.runWorker (Unknown Source) the At Java.util.concurrent.threadpoolexecutor$worker.run (Unknown Source) -At Java.lang.Thread.run (Unknown Source)

This is written in spring's documentation: the Spring AOP section uses JDK dynamic proxies or cglib to create proxies for target objects. If the target of the agent implements at least one interface, the JDK dynamic proxy is used. All interfaces implemented by this target type will be proxied. If the target object does not implement any interfaces, create a cglib proxy.

So, the solution is that if you are using a JDK dynamic proxy, you must implement an interface for the target being proxied (note that the return value of the Ctx.getbean () method needs to be received with the interface type), and if you use Cglib to force the proxy, you must import the Cglib package into the project beforehand. Sets the Proxytargetclass property of Beannameautoproxycreator to True.

Here Deviceservice implements the interface Ideviceservice, so the JDK dynamic proxy is used so that (Deviceservice) Deviceservice error when casting (because the proxy class that implements the Ideviceservice interface belongs to the Deviceservice subclass). Therefore, you need to use Cglib to force the proxy. You need to add the following configuration to the Springcontext.xml:


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

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

Java.lang.ClassCastException:com.sun.proxy. $Proxy cannot is cast to Com.bbk.n002.service.QuestionService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.