Spring中開啓事務的第三種:註解配置

Spring中開啓事務的第三種:註解配置
步驟
導包

spring-aop-4.3.8.RELEASE.jar
spring-aspects-4.3.8.RELEASE.jar
聯盟包:com.springsource.org.aopalliance-1.0.0.jar
織入包:com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-jdbc-4.3.8.RELEASE.jar
spring-tx-4.3.8.RELEASE.jar
導入tx約束

spring-tx-4.3.xsd
xmlns:tx=”http://www.springframework.org/schema/tx”

在配置文件中開啓對事務註解的支持

<!-- 開啓spring對事務的註解支持 -->
<tx:annotation-driven/>

在方法或者類上添加註解

//可以在類,屬性,方法上都可以添加註解,小範圍覆蓋大範圍的
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements IAccountService{
    ......
}

service類

//可以在類,屬性,方法上都可以添加註解,小範圍覆蓋大範圍的
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements IAccountService{

    private AccountDaoImpl ac;

    @Override
    //可以在類,屬性,方法上都可以添加註解,小範圍覆蓋大範圍的
    @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
    public void transfer(final Integer from,final Integer to,final Double money) {

        //加錢
        ac.addMoney(to, money);
//      int i=1/0;
        //減錢
        ac.decreseMoney(from, money);
    }
    public void setAc(AccountDaoImpl ac) {
        this.ac = ac;
    }

spring配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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 ">

    <!-- 加載jdbc.properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 配置事務核心管理器,封裝了所有的事務操作,依賴於連接池 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 開啓spring對事務的註解支持 -->
    <tx:annotation-driven/>


    <bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
        <property name="ac" ref="accountDaoImpl"></property>
    </bean>

    <bean id="accountDaoImpl" class="com.dao.impl.AccountDaoImpl">
        <property name="jt" ref="jdbcTemplate"></property>
    </bean>

    <!-- 配置模板對象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置連接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章