1. 程式人生 > >Spring快速筆記3

Spring快速筆記3

概述:

  • Spring的概述、SpringIOC入門(XML)、Spring的Bean管理、Spring屬性注入
  • Spring的IOC的註解方式、Spring的AOP開發(XML)
  • Spring的AOP的註解開發、Spring的宣告式事務、JdbcTemplate。
  • SSH的整合、HibernateTemplate的使用、OpenSessionInViewFilter的使用。

 

1 Spring 事務管理

 

1.1 事務

  • 事務:邏輯上的一組操作,組成這組操作的各個單元,要麼全都成功,要麼全都失敗。滿足ACID的四個特性。
    • 原子性:事務不可分割
    • 一致性:事務執行前後資料完整性保持一致
    • 隔離性:一個事務的執行不應該受到其他事務的干擾
    • 永續性:一旦事務結束,資料就持久化到資料庫
  • 事務的隔離性引發的安全性問題——讀寫:髒讀、重複讀、虛讀(幻讀)、丟失更新(寫問題);
  • 事務的隔離級別:讀未提交、讀已提交、重複讀、序列化;

 

1.2 Spring事務管理的核心API

Spring進行事務管理的時候,首先平臺事務管理器根據事務定義資訊進行事務的管理,在事務管理過程中,產生各種狀態,將這些狀態的資訊記錄到事務狀態的物件中。

  • PlatformTransactionManager:平臺事務管理器,介面,是Spring用於管理事務的真正的物件。有兩個常用實現類來完成事務的管理
    • DataSourceTransactionManager
      :底層使用JDBC管理事務
    • HibernateTransactionManager :底層使用Hibernate管理事務
  • TransactionDefinition :事務定義資訊,用於定義事務的相關的資訊:隔離級別、超時資訊、傳播行為、是否只讀
  • TransactionStatus:事務狀態:用於記錄在事務管理過程中,事務的狀態的物件

 

1.3 事務傳播行為

Spring中提供了七種事務的傳播行為,定義了事務之間的關係。事務一般在業務層完成使用,事務傳播行為主要用來解決複雜業務中多業務方法的相互呼叫問題。

  • 多個操作在同一事務中
    • PROPAGATION_REQUIRED
      :預設值,如果A中有事務,使用A中的事務,如果A沒有,建立一個新的事務,將操作包含進來
    • PROPAGATION_SUPPORTS :支援事務,如果A中有事務,用A中的事務。若A沒有事務,不使用事務。
    • PROPAGATION_MANDATORY :如果A中有事務,使用A中的事務。如果A沒有事務,丟擲異常。
  • 多個操作在不同事務中
    • PROPAGATION_REQUIRES_NEW :如果A中有事務,將A的事務掛起(暫停),建立新事務,只包含自身操作。如果A中沒有事務,建立一個新事務,包含自身操作。
    • PROPAGATION_NOT_SUPPORTED:如果A中有事務,將A的事務掛起。不使用事務管理
    • PROPAGATION_NEVER :如果A中有事務,報異常
  • 巢狀事務
    • PROPAGATION_NESTED :巢狀事務,如果A中有事務,按照A的事務執行,執行完成後,設定一個儲存點,執行B中的操作,如果沒有異常,執行通過,如果有異常,可以選擇回滾到最初始位置,也可以回滾到儲存點。

 

1.4 JDBC模板的另一種配置方式

JDBC模板使用時,需要通過Bean的方式和Set方法來進行初始化和屬性注入使用。另一種方式是,在需要使用模板的類中繼承Spring中的JdbcDaoSupport類,使用其中的JDBC模板。只需要給其注入一個DataSource即可。

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
	@Override
	public void out(String from, double money) {
		this.getJdbcTemplate().update("update user set money = money - ? where name=?", money,from);
	}
	@Override
	public void in(String to, double money) {
		this.getJdbcTemplate().update("update user set money = money + ? where name=?", money,to);
	}	
}
------------------------------------------------
<bean id="accountDao" class="com.leehao.springlearning.tx1.AccountDaoImpl">
    <property name="dataSource" ref="dataSource"></property>
</bean>

 

2 Spring事務Demo

Spring的事務管理,一定是通過事務管理器來完成的。事務管理器模板是方便事務管理的一組操作。分為程式設計式事務和宣告式管理事務。

2.1 程式設計式事務

通過配置事務管理器、事務管理器模板,在使用事務的地方注入模板,來完成事務的操作。

@Override
public void transfer(String from, String to, double money) {
    //使用事務管理模板
    txTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
            accountDao.out(from, money);
            int i = 1/0;
            accountDao.in(to, money);
        }
    });		
}
---------------------------------------------------------------
<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事務管理器模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"></property>
</bean>

<bean id="accountService" class="com.leehao.springlearning.tx1.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
    <!-- 注入事務管理模板 -->
    <property name="txTemplate" ref="transactionTemplate"></property>
</bean>	

 

2.2 宣告式事務——XML

在執行操作之前開啟事務,執行完成後關閉事務,使用AOP的思想完成。

步驟:1. 事務管理器;2. 事務通知(相當於切面類);3.配置aop事務

<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事務的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- 事務管理的規則,切入點位置的事務規則是怎樣的 -->
        <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
        <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
        <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
        <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
<!-- AOP管理事務 -->
<aop:config>
    <aop:pointcut expression="execution(* com.leehao.springlearning.tx2.AccountService.*(..))" id="pointCut1"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut1"/>
</aop:config>

 

2.3 宣告式事務——註解

步驟:1.事務管理器;2.開啟事務註解;3. 業務需要的類上加註解

<!-- 事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
------------------------------------------------------------
@Transactional(isolation=Isolation.DEFAULT, propagation=Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {