Spring的初體驗-5

一. Spring的編程式事務管理(非AOP的解決方案)

缺點:非業務代碼會對目標業務代碼造成侵入。

  1. Spring配置文件(src/beans.xml)
<?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: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/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">
        
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- jdbc事務管理器 -->
    <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>
     
	<context:property-placeholder location="jdbc.properties"/>
    
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    	<constructor-arg ref="dataSource"></constructor-arg>
    </bean>
	
	<bean id="bankDao" class="com.bee.dao.impl.BankDaoImpl">
		<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
	</bean> 
	
	<bean id="bankService" class="com.bee.service.impl.BankServiceImpl">
		<property name="bankDao" ref="bankDao"></property>
		<property name="transactionTemplate" ref="transactionTemplate"></property>
	</bean> 
</beans>
  1. Service層
public class BankServiceImpl implements BankService{

	private BankDao bankDao;
	
	private TransactionTemplate transactionTemplate;
	
	public void setBankDao(BankDao bankDao) {
		this.bankDao = bankDao;
	}
	//注入事務
	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	//非業務代碼侵入了業務代碼
	@Override
	public void transferAccounts(final int count, final int userIdA, final int userIdB) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				bankDao.outMoney(count, userIdA);
				bankDao.inMoney(count, userIdB);				
			}
		});
	}
}

二. Spring的聲明式事務管理(AOP解決方案)

注意:AOP只能作用於方法。

  1. 使用XML配置文件聲明事務(可批量處理實現類)
  • Spring配置文件(src/beans.xml)
<?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:context="http://www.springframework.org/schema/context"
    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/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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- jdbc事務管理器 -->
    <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="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>
    
    <!-- 配置事務切面 -->
    <aop:config>
    	<!-- 配置切點:切入service層的方法。
    		掃描具體是匹配到包下的接口(看接口中定義的抽象方法)。 
    		注意Spring要求針對接口編程。
    	-->
    	<aop:pointcut id="serviceMethod" expression="execution(* com.bee.service.*.*(..))" />
    	<!-- 配置事務通知 -->
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>
    
	<context:property-placeholder location="jdbc.properties"/>
    
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    	<constructor-arg ref="dataSource"></constructor-arg>
    </bean>
	
	<bean id="bankDao" class="com.bee.dao.impl.BankDaoImpl">
		<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
	</bean> 
	
	<bean id="bankService" class="com.bee.service.impl.BankServiceImpl">
		<property name="bankDao" ref="bankDao"></property>
	</bean> 
	
</beans>
  • Service層
public class BankServiceImpl implements BankService{

	private BankDao bankDao;
	//DI
	public void setBankDao(BankDao bankDao) {
		this.bankDao = bankDao;
	}
	
	@Override
	public void transferAccounts(int count, int userIdA, int userIdB) {
		bankDao.outMoney(count, userIdA);
		bankDao.inMoney(count, userIdB);				
	}
}
  1. 使用註解聲明事務(針對單個實現類)
  • Spring配置文件(src/beans.xml)
<?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:context="http://www.springframework.org/schema/context"
    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/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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- jdbc事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 聲明註解事務 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
     
	<context:property-placeholder location="jdbc.properties"/>
    
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    	<constructor-arg ref="dataSource"></constructor-arg>
    </bean>
		
	<bean id="bankDao" class="com.bee.dao.impl.BankDaoImpl">
		<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
	</bean> 
	
	<bean id="bankService" class="com.bee.service.impl.BankServiceImpl">
		<property name="bankDao" ref="bankDao"></property>
	</bean> 
</beans>
  • Service層
@Transactional
public class BankServiceImpl implements BankService{
	private BankDao bankDao;
	//DI
	public void setBankDao(BankDao bankDao) {
		this.bankDao = bankDao;
	}
	
	@Override
	public void transferAccounts(int count, int userIdA, int userIdB) {
		bankDao.outMoney(count, userIdA);
		bankDao.inMoney(count, userIdB);				
	}
}

三. 事務傳播行爲

Spring 中當一個 service 方法調用另外一個 service 方法的時候,因爲每個 service 方法都有事務,這時候就出現了事務的嵌套,由此,就產生了事務傳播行爲。

在 Spring 中通過配置屬性 propagation,來定義事務傳播行爲:

  • PROPAGATION_REQUIRED:支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
  • PROPAGATION_SUPPORTS:支持當前事務,如果當前沒有事務,就以非事務方式執行。
  • PROPAGATION_MANDATORY:支持當前事務,如果當前沒有事務,就拋出異常。
  • PROPAGATION_REQUIRES_NEW:新建事務,如果當前存在事務,把當前事務掛起。
  • PROPAGATION_NOT_SUPPORTED:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
  • PROPAGATION_NEVER:以非事務方式執行,如果當前存在事務,則拋出異常。

Spring中的常用配置:

<tx:attributes>
	<tx:method name="insert*" propagation="REQUIRED" />
	<tx:method name="update*" propagation="REQUIRED" />
	<tx:method name="edit*" propagation="REQUIRED" />
	<tx:method name="save*" propagation="REQUIRED" />
	<tx:method name="add*" propagation="REQUIRED" />
	<tx:method name="new*" propagation="REQUIRED" />
	<tx:method name="set*" propagation="REQUIRED" />
	<tx:method name="remove*" propagation="REQUIRED" />
	<tx:method name="delete*" propagation="REQUIRED" />
	<tx:method name="change*" propagation="REQUIRED" />
	<tx:method name="get*" propagation="REQUIRED" read-only="true" />
	<tx:method name="find*" propagation="REQUIRED" read-only="true" />
	<tx:method name="load*" propagation="REQUIRED" read-only="true" />
	<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章