1. 程式人生 > >Spring框架的事務管理之基於AspectJ的註解方式(重點掌握,最簡單的方式)

Spring框架的事務管理之基於AspectJ的註解方式(重點掌握,最簡單的方式)

1. 步驟一:恢復轉賬的開發環境(具體開發環境實現見:https://www.cnblogs.com/wyhluckdog/p/10137283.html
2. 步驟二:applicationContext的基本配置為:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
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"
> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/> <property name="user" value="root"/> <
property name="password" value="root"/> </bean> </beans>
3. 步驟三:配置事務管理器
<!-- 配置事務管理器 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"/>
     </bean>
4. 步驟四:開啟註解事務
<!-- 開啟註解事務 -->
      <tx:annotation-driven transaction-manager="transactionManager"/>
5.完整的applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
    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">
     
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name="driverClass" value="com.mysql.jdbc.Driver"/>
         <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
         <property name="user" value="root"/>
         <property name="password" value="root"/>
     </bean>
    
        <!-- 配置事務管理器 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"/>
     </bean>
  <!--      
    開啟註解事務 -->
      <tx:annotation-driven transaction-manager="transactionManager"/>
      
    <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
     <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
            <property name="accountDao" ref="accountDao"/>
           <!--  <property name="transactionTemplate" ref="transactionTemplate"/> -->
     </bean>

    
     
</beans>
6.步驟五:在業務層上新增一個註解:@Transactional。
  * 以下是AccountServiceImpl的完整程式碼為:
package com.huida.demo1;

import javax.annotation.Resource;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

@Transactional
public class AccountServiceImpl implements AccountService{

    @Resource(name="accountDao")
    private AccountDaoImpl accountDao;
    

    public void setAccountDao(AccountDaoImpl accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    public void pay(String out,String in,double money) {

        //扣錢
        accountDao.outMoney(out, money);
        //加錢
        accountDao.inMoney(in, money);
    }

}
7. 步驟六:編寫測試類
package com.huida.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {

    @Resource(name="accountService")
    private AccountService accountService;
    
    @Test
    public void run1(){
        accountService.pay("小明","小紅",1000);
    }
}
8.單元測試run1方法,重新整理spring-day03資料庫中的user表,可以看到小明的money減少了1000,小紅的money增加了1000.