Java. Lang. classcastexception: $ proxy17 cannot be cast to *. Service. impl. deviceservice

Source: Internet
Author: User

Error:

 

Java. lang. classcastexception: $ proxy17 cannot be cast *. *. *. service. impl. deviceserviceat *. *. *. service. impl. deviceservicetest. testdeviceisonline (deviceservicetest. java: 436) at Sun. reflect. nativemethodaccessorimpl. invoke0 (native method) at Sun. reflect. nativemethodaccessorimpl. invoke (nativemethodaccessorimpl. java: 39) at Sun. reflect. delegatingmethodaccessorimpl. invoke (delegatingmethodaccessorimpl. java: 25) at java. lang. reflect. method. invoke (method. java: 597) at Org. JUnit. runners. model. frameworkmethod $1. runreflectivecall (frameworkmethod. java: 47) at Org. JUnit. internal. runners. model. reflectivecallable. run (reflectivecallable. java: 12) at Org. JUnit. runners. model. frameworkmethod. invokeexplosively (frameworkmethod. java: 44) at Org. JUnit. internal. runners. statements. invokemethod. evaluate (invokemethod. java: 17) at Org. JUnit. internal. runners. statements. runbefores. evaluate (runbefores. java: 26) at Org. springframework. test. context. junit4.statements. runbeforetestmethodcallbacks. evaluate (runbeforetestmethodcallbacks. java: 74) at Org. springframework. test. context. junit4.statements. runaftertestmethodcallbacks. evaluate (runaftertestmethodcallbacks. java: 83) at Org. springframework. test. context. junit4.statements. springrepeat. evaluate (springrepeat. java: 72) at Org. springframework. test. context. junit4.springjunit4classrunner. runchild (springjunit4classrunner. java: 231) at Org. JUnit. runners. blockjunit4classrunner. runchild (blockjunit4classrunner. java: 50) at Org. JUnit. runners. parentrunner $ 3.run( parentrunner. java: 238) at Org. JUnit. runners. parentrunner $1. schedule (parentrunner. java: 63) at Org. JUnit. runners. parentrunner. runchildren (parentrunner. java: 236) at Org. JUnit. runners. parentrunner. access $000 (parentrunner. java: 53) at Org. JUnit. runners. parentrunner $2. evaluate (parentrunner. java: 229) at Org. springframework. test. context. junit4.statements. runbeforetestclasscallbacks. evaluate (runbeforetestclasscallbacks. java: 61) at Org. springframework. test. context. junit4.statements. runaftertestclasscallbacks. evaluate (runaftertestclasscallbacks. java: 71) at Org. JUnit. runners. parentrunner. run (parentrunner. java: 309) at Org. springframework. test. context. junit4.springjunit4classrunner. run (springjunit4classrunner. java: 174) at Org. eclipse. jdt. internal. junit4.runner. junit4testreference. run (junit4testreference. java: 50) at Org. eclipse. jdt. internal. JUnit. runner. testexecution. run (testexecution. java: 38) at Org. eclipse. jdt. internal. JUnit. runner. remotetestrunner. runtests (remotetestrunner. java: 467) at Org. eclipse. jdt. internal. JUnit. runner. remotetestrunner. runtests (remotetestrunner. java: 683) at Org. eclipse. jdt. internal. JUnit. runner. remotetestrunner. run (remotetestrunner. java: 390) at Org. eclipse. jdt. internal. JUnit. runner. remotetestrunner. main (remotetestrunner. java: 197)

Cause:

 

 

Written in the spring document: Spring AOP uses JDK dynamic proxy or cglib to create a proxy for the target object. If the proxy target implements at least one interface, JDK dynamic proxy is used. All interfaces implemented for this target type will be proxies. If the target object does not implement any interfaces, A cglib proxy is created.

Therefore, the solution is that if JDK dynamic proxy is used, an interface must be implemented for the target to be proxy (note that CTX is required. the Return Value of the getbean () method is received by the interface type). If you use the cglib force proxy, you must import the cglib package to the project and set the proxytargetclass attribute of beannameautoproxycreator to true.

Here, deviceservice implements the interface IDeviceservice.JDK dynamic proxy, so that (deviceservice) deviceservice forced conversion error (because the implementation IDThe eviceservice interface proxy class isDEviceservice subclass). Therefore, you need to useCglib enforces proxy. Add the following configuration in springcontext. xml:

 

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

 

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.