Spring中开启事务的第二种,通过aop配置

Spring中开启事务的第二种,通过aop配置

步骤

  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:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
        </tx:attributes>
    </tx:advice>
  4. 配置织入

    <!-- 配置织入 -->
    <aop:config>
        <aop:pointcut expression="execution( * com.service.impl.*ServiceImpl.*(..))" id="txpc"/>
    
        <!-- 建立事务管理器对象,与切入点表达式的关系 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
    </aop:config>

总配置文件

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


    <!-- 配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置织入 -->
    <aop:config>
        <aop:pointcut expression="execution( * com.service.impl.*ServiceImpl.*(..))" id="txpc"/>

        <!-- 建立事务管理器对象,与切入点表达式的关系 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
    </aop:config>



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

service方法

public class AccountServiceImpl implements IAccountService{

    private AccountDaoImpl ac;

    @Override
    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;
    }
}

测试类

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

    @Resource(name="accountServiceImpl")
    private IAccountService as;

    @Test
    public void test1(){
        as.transfer(1, 2, 100d);
    }
}

猜你喜欢

转载自blog.csdn.net/kato_op/article/details/80247823