聲明式事務管理:(自動代理.基於切面)
第一步:導入相應jar包.
aspectj
第二步:引入相應約束:
* aop、tx約束
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
第三步:注冊事務管理器;
<!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
第四步:定義增強(事務管理)
<!-- 定義一個增強 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 增強(事務)的屬性的配置 --> <tx:attributes> <!-- isolation:DEFAULT :事務的隔離級別. propagation :事務的傳播行為. read-only :false.不是只讀 timeout :-1 no-rollback-for :發生哪些異常不回滾 rollback-for :發生哪些異常回滾事務 --> <tx:method name="transfer"/> </tx:attributes> </tx:advice>
第五步:定義aop的配置(切點和通知的組合)
<!-- aop配置定義切面和切點的信息 --> <aop:config> <!-- 定義切點:哪些類的哪些方法應用增強 --> <aop:pointcut expression="execution(* cn.itcast.spring3.demo3.AccountService+.*(..))" id="mypointcut"/> <!-- 定義切面: --> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/> </aop:config>
聲明式事務管理--完整版配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 引入外部屬性文件. --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 業務層類 --> <bean id="accountService" class="cn.itcast.spring3.demo3.AccountServiceImpl"> <!-- 在業務層注入Dao --> <property name="accountDao" ref="accountDao"/> </bean> <!-- 持久層類 --> <bean id="accountDao" class="cn.itcast.spring3.demo3.AccountDaoImpl"> <!-- 注入連接池的對象,通過連接池對象創建模板. --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 定義一個增強 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 增強(事務)的屬性的配置 --> <tx:attributes> <!-- isolation:DEFAULT :事務的隔離級別. propagation :事務的傳播行為. read-only :false.不是只讀 timeout :-1 no-rollback-for :發生哪些異常不回滾 rollback-for :發生哪些異常回滾事務 --> <tx:method name="transfer"/> </tx:attributes> </tx:advice> <!-- aop配置定義切面和切點的信息 --> <aop:config> <!-- 定義切點:哪些類的哪些方法應用增強 --> <aop:pointcut expression="execution(* cn.itcast.spring3.demo3.AccountService+.*(..))" id="mypointcut"/> <!-- 定義切面: --> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/> </aop:config> </beans>
第六步:編寫測試類:
* 注入Service對象,不需要注入代理對象(生成這個類的時候,已經是代理對象.)
package cn.itcast.spring3.demo3; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 聲明式事務使用:基於切面的 * @author 姜濤 * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext3.xml") public class SpringTest3 { @Autowired @Qualifier("accountService") private AccountService accountService; @Test public void demo1(){ accountService.transfer("aaa", "bbb", 100d); } }