Java.lang.ClassCastException:com.sun.proxy. $Proxy 2 cannot is cast to ... Abnormal

Source: Internet
Author: User

Abnormal:

Exception in thread "main" java.lang.ClassCastException:com.sun.proxy. $Proxy 2 cannot is cast to Com.pro.service.impl.UserServiceImpl
At Com.pro.test.TestSpring.main (testspring.java:12)

first, the project of throwing anomalies

Defining the service Layer interface

1  PackageCom.pro.service;2 3 /**4 * User Operation interface5  */6  Public InterfaceIuserservice {7      Public voidAdd ();//Add Method8      Public voidUpdate ();//Modify Method9      Public voidDelete ();//Delete MethodTen      Public voidQuery ();//Query Method One}

defining service Layer Implementation classes

 PackageCom.pro.service.impl;ImportCom.pro.service.IUserService; Public classUserserviceimplImplementsIuserservice {@Override Public voidAdd () {System.out.println ("Add Method"); } @Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("Modify Method"); } @Override Public voidDelete () {System.out.println ("Delete Method"); } @Override Public voidquery () {System.out.println ("Query Method"); }}
View Code

Configuration file Code

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring- Aop-3.0.xsd "> <ImportResource= "Config/user.xml"/> <BeanID= "UserService"class= "Com.pro.service.impl.UserServiceImpl"></Bean> <BeanID= "Log"class= "Com.pro.aop.Log"></Bean>  <BeanID= "Logtwo"class= "Com.pro.aop.LogTwo"></Bean> <BeanID= "Logthree"class= "Com.pro.aop.LogThree"></Bean> <!--1.api using AOP - <Aop:config>     <Aop:pointcutID= "Pointcut"expression= "Execution (* com.pro.service.impl.*.* (..))"/>     <Aop:advisorAdvice-ref= "Log"Pointcut-ref= "Pointcut"/> </Aop:config>  </Beans>

Test code

1  Packagecom.pro.test;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 ImportCom.pro.service.IUserService;7 ImportCom.pro.service.impl.UserServiceImpl;8 9  Public classtestspring {Ten      Public Static voidMain (string[] args) { OneApplicationContext ac=NewClasspathxmlapplicationcontext ("Config + +"); AUserserviceimpl u= (Userserviceimpl) Ac.getbean ("UserService"); - U.add (); -     } the}

Second, reason analysis

Spring AOP is implemented in two ways, one using the JDK dynamic agent, and the other through cglib to create proxies for the target object. If the target of the agent implements at least one interface, the JDK dynamic proxy is used, and all interfaces implemented by that target type are proxied. If the target object does not implement any interfaces, a CGLIB proxy is created and the proxy class created is a subclass of the target class.

Obviously, this project implements an interface, so it is through the JDK dynamic agent to implement AOP.

Third, the solution

1. Configure proxy-target-class="true" in the configuration file

<proxy-target-class= "true"/>

2. Change the target type to interface type

Java.lang.ClassCastException:com.sun.proxy. $Proxy 2 cannot is cast to ... Abnormal

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.