Spring中开启事务的第三种:注解配置

Spring中开启事务的第三种:注解配置

步骤

  1. 导包

    • 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
  2. 导入tx约束

    • spring-tx-4.3.xsd
    • xmlns:tx=”http://www.springframework.org/schema/tx”
  3. 在配置文件中开启对事务注解的支持

    
    <tx:annotation-driven/>
  4. 在方法或者类上添加注解

    //可以在类,属性,方法上都可以添加注解,小范围覆盖大范围的
    @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;
    }
}

总配置文件


<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 ">

    
    <context:property-placeholder location="classpath:jdbc.properties" />

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

    
    <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>

你可能感兴趣的:(spring)