1. 程式人生 > >Spring在xml配置裡配置事務

Spring在xml配置裡配置事務

事先準備:
配置資料來源物件
用<bean>例項化各個業務物件。

1.配置事務管理器。

<bean id="transactionManager" class="org.springframework.jdbc.datasourceManager">
  <property name="datasource" ref="datasource"></property>
</bean>

 

2.配置事務屬性

複製程式碼
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="方法名" propagation="REQUIRES_NEW"/>    <!--新開事務-->
        <tx:method name="*"/>                                    <!--使用原有事務-->
    </tx:attributes>
</tx:advice>
複製程式碼

 

3.配置事務切入點,注入事務屬性

<aop:config>
    <aop:pointcut expression="execution(.......)" id="txPointCut"/>
    <aop:advisor advice-ref="txtAdvice" pointcut-ref="txtPointCut"/>
</aop:config>

例項:

準備工作:匯入c3p0、Spring框架、Mysql、AOP的jar包,並配置好。

db.properties 

複製程式碼
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb
user=root
password=
minPoolSize=5
maxPoolSize=20
initialPoolSize=5
複製程式碼

三個介面

package com.itnba.maya.dao;

public interface IInfoDao {
    public void delete(String code);
}
package com.itnba.maya.dao;

public interface IWorkDao {
    public void deleteInfocode(String code);
}
複製程式碼
package com.itnba.maya.dao;

public interface IInfoService {
    public void delete(String code);

}
複製程式碼

介面的實現類

複製程式碼
package com.itnba.maya.daoimp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.itnba.maya.dao.IInfoDao;

public class InfoDao implements IInfoDao {

    private JdbcTemplate j;
    public JdbcTemplate getJ() {
        return j;
    }
    public void setJ(JdbcTemplate j) {
        this.j = j;
    }
    @Override
    public void delete(String code) {
        // 故意設定一個錯誤
        if(code.equals("p008")){
            int n=1/0;
        }
        
        String sql="delete from info where code=?";
        j.update(sql,code);
    }


}
複製程式碼 複製程式碼
package com.itnba.maya.daoimp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.itnba.maya.dao.IWorkDao;

public class WorkDao implements IWorkDao {
    
    private JdbcTemplate j;

    public JdbcTemplate getJ() {
        return j;
    }

    public void setJ(JdbcTemplate j) {
        this.j = j;
    }

    public void deleteInfocode(String code) {
    
        String sql="delete from work where infocode=?";
        j.update(sql,code);
    }

}
複製程式碼 複製程式碼
package com.itnba.maya.daoimp;

import com.itnba.maya.dao.IInfoDao;
import com.itnba.maya.dao.IInfoService;
import com.itnba.maya.dao.IWorkDao;

public class InfoService implements IInfoService {
    
    private IInfoDao infoDao; 
    public IInfoDao getInfoDao() {
        return infoDao;
    }
    public void setInfoDao(IInfoDao infoDao) {
        this.infoDao = infoDao;
    }
    public IWorkDao getWorkdao() {
        return workdao;
    }
    public void setWorkdao(IWorkDao workdao) {
        this.workdao = workdao;
    }
    private IWorkDao workdao;
    public void delete(String code) {
        
        infoDao.delete(code);
        workdao.deleteInfocode(code);
        
    }

}
複製程式碼

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:tx="http://www.springframework.org/schema/tx"
    default-autowire="byName"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        
        <!-- 引入db.properties檔案 -->
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 生成連線池 -->
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="driverClass" value="${driverClass}"></property>
            <property name="jdbcUrl" value="${jdbcUrl}"></property>
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            <property name="minPoolSize" value="${minPoolSize}"></property>
            <property name="maxPoolSize" value="${maxPoolSize}"></property>
            <property name="initialPoolSize" value="${initialPoolSize}"></property>
        </bean>
        <!-- 生成JdbcTemplate -->
        <bean class="org.springframework.jdbc.core.JdbcTemplate" id="j">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 配置實體類 -->
        <bean class="com.itnba.maya.daoimp.InfoDao" id="infoDao">
            <property name="j" ref="j"></property>
        </bean>    
        <bean class="com.itnba.maya.daoimp.WorkDao" id="workDao">
            <property name="j" ref="j"></property>
        </bean>
            <bean class="com.itnba.maya.daoimp.InfoService" id="service">
            <property name="infoDao" ref="infoDao"></property>
            <property name="workdao" ref="workDao"></property>
        </bean>
        
        
        <!-- 配置事務管理器 -->
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <tx:advice id="advice"     transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED"/><!-- *是對所有方法都加 -->
            </tx:attributes>
        </tx:advice>
        <!-- 用切點把事務切進去 -->
        <aop:config>
            <aop:pointcut expression="execution(* com.itnba.maya.daoimp..*.*(..))" id="pointcut"/>
            <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
        </aop:config>
        
</beans>
複製程式碼

mian函式測試事務有沒有生效:

複製程式碼
package com.itnba.maya.daoimp;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itnba.maya.dao.IInfoService;

public class Test {
    private static ApplicationContext context=null;

    private static IInfoService infoservice=null;

    
    
    static{
        context=new ClassPathXmlApplicationContext("beans.xml");
        infoservice=(IInfoService) context.getBean("service");
        
    }
    
    public static void main(String[] args) {
        
        infoservice.delete("p008");


    }

}
複製程式碼

結果除0錯誤,資料回滾,資料庫並沒有刪除。說明配置的事務生效了。