1. 程式人生 > >spring框架(四)——Spring中的事務控制

spring框架(四)——Spring中的事務控制

注意:spring的jdbc模組筆者這裡不做詳解, 因為實際中用的不多,(不過還真有用的,筆者這裡什麼時候用什麼時候在整理),那麼這個模組的事務,要做一下整理。

程式設計式事務、這裡不講,就是將事務的開啟關閉寫在程式碼裡。不做重點。

1 spring的宣告式事務控制(重點)

程式設計式事務管理將資料層提交事務的程式碼加入到邏輯層,與Spring無侵入式程式設計的主思想有衝突,實際開發過程中,往往採用宣告式事務管理形式
通過程式設計式事務管理的程式碼不難看出,在業務邏輯層對應的業務上新增某些程式碼即可完成整體事務管理的操作,使用SpringAOP的思想,將公共的程式碼加入後,即可完成對應的工作,這就是宣告式事務管理的核心機制。

1.1 基於XML的宣告式事務配置

a、匯入spring事務支援的jar包spring-tx-3.2.0.RELEASE.jar

b、引入spring事務的名稱空間


c、配置spring的事務支援

<?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: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/tx 
      					   http://www.springframework.org/schema/tx/spring-tx.xsd
      					   http://www.springframework.org/schema/aop 
      					   http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 把Dao交給Spring管理 -->
    <bean id="accountDao" class="springTX.dao.impl.AccountDaoImpl">
    	<!-- 為dao的父類JdbcDaoSupport注入一個數據源 -->
    	<property name="dataSource" ref="driverManagerDataSource"></property>
    </bean>
    
    <!-- 把業務層也交給Spring -->
    <bean id="accountService" class="springTX.service.impl.AccountServiceImpl">
    	<property name="accountDao" ref="accountDao"></property>
    </bean>

	<!-- 事務控制:宣告式事務,重點。基於XML的。
		 前期準備:
		 	1.匯入aop的jar包
		 	2.確保spring-tx-xxxx.jar在你的工作空間中
		 步驟:
		 	1.配置一個事務管理器,併為其注入資料來源
		 	2.配置事務的通知.id是為其指定一個唯一標識。transaction-manager指定事務通知使用的管理器
		 	3.配置AOP,指定切面使用的通知。<aop:advisor advice-ref="" pointcut="切入點表示式"/>它就是指定已經配置好的通知型別。
	-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	<tx:advice id="txAdivce" transaction-manager="transactionManager">
		<!-- 配置事務的相關屬性 -->
		<tx:attributes>
			<!-- tx:method 是用於指定方法名稱和事務屬性的關係。
				 name:指定方法的名稱。可以使用萬用字元*,也可以部分通配
				 isolation:事務的隔離級別。預設值是:DEFAULT。當是default時是看資料庫的隔離級別。 
				 rollback-for:該屬性用於配置一個異常,當產生該異常時回滾。
				 no-rollback-for:該屬性用於配置一個異常,當產生該異常時不回滾。
				 propagation:指定事務的傳播行為。 預設值是:REQUIRED
				 read-only:指定當前事務是否是隻讀事務。預設值是false,不是隻讀的。
				 timeout:指定超時時間。預設值是-1。以秒為單位
			 -->
			<tx:method name="*" />
			<tx:method name="find*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:advisor advice-ref="txAdivce" pointcut="execution(* springTX..*.*(..))"/>
	</aop:config>
	<!-- 配置Spring的內建資料來源 -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 給資料來源注入引數 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/spring_tx"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
</beans>
解釋如下圖:

OK、基於xml的宣告式事務、配置完畢。

2 基於註解的宣告式事務配置

a、把spring容器管理改為註解配置的方式(開啟要掃描的包)

b、開啟註解事務的支援


配置結果如下:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
      					   http://www.springframework.org/schema/beans/spring-beans.xsd 
      					   http://www.springframework.org/schema/tx 
      					   http://www.springframework.org/schema/tx/spring-tx.xsd
      					   http://www.springframework.org/schema/aop 
      					   http://www.springframework.org/schema/aop/spring-aop.xsd 
      					   http://www.springframework.org/schema/context 
      					   http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 事務控制:宣告式事務,重點。基於註解的。
		 前期準備:
		 	1.匯入context的名稱空間
		 	2.確保spring-tx-xxxx.jar在你的工作空間中
		 步驟:
		 	1.設定Spring要掃描的包
		 	2.開始Spring對註解式事務的支援。並且指定事務管理器類
		 	3.把Dao和service都交給Spring管理
		 	4.由於我們使用了註解,所以不能用JdbcDaoSupport,這個時候我們需要定義JdbcTemplate,為其注入資料來源
	-->
    <context:component-scan base-package="springTX"></context:component-scan>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="driverManagerDataSource"></property>
	</bean>
	
	<!-- 配置Spring的內建資料來源 -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 給資料來源注入引數 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/srping_tx"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
</beans>
c、在需要事務支援的地方加入@Transactional註解(一般在service層控制事務)
d、@Transactional註解配置的位置

  • 用在業務實現類上:該類所有的方法都在事務的控制範圍

  • 用在業務介面上:該介面的所有實現類都起作用

  • 通過註解指定事務的定義資訊

OK、基於註解的事務也整理完畢!

注意:對於事務上還有什麼不瞭解的地方,可以觀看筆者整理的spring==註解事務問題以及xml事務的配置、

url:http://blog.csdn.net/sinat_38259539/article/details/77678557