Java.lang.ClassCastException:com.sun.proxy. $Proxy cannot is cast to Sm.dao.UserDao

Source: Internet
Author: User

Issues that occur after you add a thing management in spring

Source

New Classpathxmlapplicationcontext ("Applicationcontext.xml"= (Userdao) applicationcontext.getbean ("UserDao" );

Error reasons for spring AOP using two proxy methods, one is a regular JDK, one is cglib, my Userdao an interface Iuserdao, when the proxy object implements at least one interface, the default use JDK dynamically create proxy object, The Cglib method is used when the proxy object does not implement any interfaces. Specific introduction of the article

Workaround:

Because JDK-based proxies are interface-oriented, we modify the code ourselves as follows

   New Classpathxmlapplicationcontext ("Applicationcontext.xml");     = (Iuserdao) applicationcontext.getbean ("Userdao");

Attach Applicationcontext.xml Configuration

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JDBC http://www.springframework.org/schema/jdbc/spring-jdbc.xsd/HTTP Www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">    <BeanID= "Datasoure"class= "Org.apache.commons.dbcp.BasicDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property>        < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/sm"></ Property>        < Propertyname= "username"value= "root"></ Property>        < Propertyname= "Password"value= "Genji"></ Property>    </Bean>    <BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean">        < Propertyname= "DataSource">            <refBean= "Datasoure"></ref>        </ Property>        < Propertyname= "Hibernateproperties">            <Props>                <propKey= "Hibernate.dialect">Org.hibernate.dialect.MySQL5Dialect</prop>                <propKey= "Hibernate.current_session_context_class">Org.springframework.orm.hibernate4.SpringSessionContext</prop>            </Props>        </ Property>        < Propertyname= "Mappingresources">            <List>                <value>Sm/po/studentinfo.hbm.xml</value>                <value>Sm/po/user.hbm.xml</value>            </List>        </ Property>    </Bean>    <BeanID= "Userdao"class= "Sm.dao.UserDao">        < Propertyname= "Sessionfactory">            <refBean= "Sessionfactory"></ref>        </ Property>    </Bean>    <BeanID= "Sinfodao"class= "Sm.dao.SinfoDao">        < Propertyname= "Sessionfactory">            <refBean= "Sessionfactory"></ref>        </ Property>    </Bean>
This is where the transaction agent is implemented.
<BeanID= "Txmanager"class= "Org.springframework.orm.hibernate4.HibernateTransactionManager"> < Propertyname= "Sessionfactory"ref= "Sessionfactory" /> </Bean> <Tx:annotation-driven/> <Tx:adviceID= "Txadvice"Transaction-manager= "Txmanager"> <tx:attributes> <Tx:methodname= "Add"read-only= "true"Propagation= "REQUIRED" /> <Tx:methodname= "read"Propagation= "REQUIRED" /> <Tx:methodname= "Del"Propagation= "REQUIRED" /> <Tx:methodname= "mod"Propagation= "REQUIRED" /> <Tx:methodname= "Sameid"Propagation= "REQUIRED" /> <Tx:methodname= "Finduser"Propagation= "REQUIRED"/> <Tx:methodname= "AddUser"Propagation= "REQUIRED"></Tx:method> </tx:attributes> </Tx:advice></Beans>

Related articles

http://cheneyjuu.blog.163.com/blog/static/41917640201051042941159/

If you have any questions about the article, you can write my Weibo.

Java.lang.ClassCastException:com.sun.proxy. $Proxy cannot is cast to Sm.dao.UserDao

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.