这次呢,我要用申明式的JDBC事务管理机制,来为IBookDao类的方法添加事务功能,注意,我不需要修改 Dao文件,只需要添加点东西到配置文件中;同时呢在测试时候获取到这个代理者TransactionProxyFactoryBean的实例,而不是BookDao2实例。剩下都不用变啦,看来蛮方便,嘿嘿:》
<?xml version="1.0" encoding="GBK"?><!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/yay</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>qwe123</value> </property></bean>
<bean id="bookDao" class="com.yinbodotcc.BookDao"> <property name="ds"> <ref bean="datasource"/> </property></bean>
<bean id="bookDao2" class="com.yinbodotcc.BookDao2"> <property name="ds"> <ref bean="datasource"/> </property></bean>
<bean id="userDao" class="com.yinbodotcc.Transaction.UserDao"> <property name="ds"> <ref bean="datasource"/> </property></bean>
<!-- 下面呢,我要给BookDao2加上声明式的事务功能,请注意,我不需要修改BookDao2 只需要在测试文件中改获取BookDao2对象为获取这里声明的代理者对象 也就是说把IBookDao ib=(IBookDao)ac.getBean("bookDao2"); 换成IBookDao ib=(IBookDao)ac.getBean("bookDao2Proxy");--><bean id="bookDao2Proxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="proxyInterfaces"> <list> <value>com.yinbodotcc.IBookDao</value> </list> </property> <property name="target"> <ref bean="bookDao2"/> </property> <!--transactionManager这里也要添加一个实例,见后面--> <property name="transactionManager"> <ref bean="dataSourceTransactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="datasource"/> </property></bean>
</beans>