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

Source: Internet
Author: User

1 severity: servlet/n002-1.0 threw Load () exception 2 java.lang.ClassCastException:com.sun.proxy. $Proxy cannot is cast to co M.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.ca Talina.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) at Org.apache.catalina.util.LifecycleBase.start (lifecyclebase.java:150) at Org.apache.catalina.core.ContainerBase $StartChild. Call (containerbase.java:1559) at 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) 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. For example, the Serviceimpl Class I wrote myself implements the service interface, and when I use Getbean (), I can't use Serviceimpl to receive it, but I use the service interface to receive

    Salaryservice Salaryservice = (salaryservice) New Classpathxmlapplicationcontext ("Bean.xml"). GetBean ("SalaryService ");

  

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

Related Article

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.