在Spring中添加事物管理之後出現的問題mysql
源代碼spring
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("UserDao");
錯誤緣由對於Spring AOP 採用兩種代理方法,一種是常規JDK,一種是CGLIB,個人UserDao了一個接口IUserDao,當代理對象實現了至少一個接口時,默認使用JDK動態建立代理對象,當代理對象沒有實現任何接口時,就會使用CGLIB方法。具體介紹文章 sql
解決方法:apache
由於基於JDK的代理是面向接口的,因此咱們本身修改代碼以下session
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserDao userDao = (IUserDao) applicationContext.getBean("UserDao");
附上applicationContext.xml配置app
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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"> <bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/sm"></property> <property name="username" value="root"></property> <property name="password" value="genji"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSoure"></ref> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop> <prop key="hibernate.current_session_context_class"> org.springframework.orm.hibernate4.SpringSessionContext </prop> </props> </property> <property name="mappingResources"> <list> <value>sm/po/Studentinfo.hbm.xml</value> <value>sm/po/User.hbm.xml</value> </list> </property> </bean> <bean id="UserDao" class="sm.dao.UserDao"> <property name="sessionFactory"> <ref bean="sessionFactory"></ref> </property> </bean> <bean id="SinfoDao" class="sm.dao.SinfoDao"> <property name="sessionFactory"> <ref bean="sessionFactory"></ref> </property> </bean>
//就是在這裏實現了事務代理
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven /> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add" read-only="true" propagation="REQUIRED" /> <tx:method name="read" propagation="REQUIRED" /> <tx:method name="del" propagation="REQUIRED" /> <tx:method name="mod" propagation="REQUIRED" /> <tx:method name="sameId" propagation="REQUIRED" /> <tx:method name="findUser" propagation="REQUIRED"/> <tx:method name="addUser" propagation="REQUIRED"></tx:method> </tx:attributes> </tx:advice> </beans>
相關文章ui
http://cheneyjuu.blog.163.com/blog/static/41917640201051042941159/url
對文章有任何疑問能夠私信個人微博spa