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