基于XML的声明式事务控制

基于XML的声明式事务控制

1.什么是声明式事务控制

采用声明的方式俩处理事务。声明就是在配置文件中声明,用Spring配置文件中声明式的处理事务来代替代码式的处理事务

声明式事务处理的作用
  • 事务管理不侵入开发组件,业务逻辑不会意识到正在事务管理。事务管理属于系统层面的服务,而不是业务逻辑的一部分,如果想要修改事务,在定义文件中重新配置即可
  • 在管理事务时,只要在设定文件上修改一下即可移去事务

注意:Spring声明式事务控制底层就是AOP

事务控制的AOP思想

​ 切点:具体业务中的方法

​ 被增强的方法:业务方法

示例:

// controller 层进行调用
public class TestContrller {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        AccountService accountService = (AccountService) applicationContext.getBean("accountService");
        accountService.transfer("tom","lucy",1000);
    }

}
// 对应数据库的表的实体类
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Account {
    private String name;
    private double money;
}
// service 层接口
public interface AccountService {
    void transfer(String outMan,String inMan,double money);
}

// service 层实现类
@Setter
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    @Override
    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }
}

// dao 层接口
public interface AccountDao {
    void out(String outMan , double money);
    void in(String inMan , double money);
}
// dao 层实现类
@Setter
public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;


    @Override
    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money = money - ? where name  = ?",money,outMan);
    }

    @Override
    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money = money + ? where name  = ?",money,inMan);
    }
}

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ajax
jdbc.username = root
jdbc.password = root
<?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: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.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.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="accountService" class="com.itheima.service.impl.AccountServiceImpl">
            <property name="accountDao" ref="accountDao"/>
        </bean>
        <!-- 目标对象   内部的方法就是切入点 -->
        <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"/>
        </bean>
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 配置平台事务管理器 -->
        <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- 配置数据源 -->
        <!-- 读取properties文件中的数据 -->
        <context:property-placeholder location="c3p0config.properties"/>
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="driverClass" value="${jdbc.driver}"/>
        </bean>

        <!-- 通知 事务的增强 -->
        <tx:advice id="txAdvice" transaction-manager="transcationManager">
            <tx:attributes>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>


        <!-- 织入 将通知与切面结合 -->
        <aop:config>
            <!-- 事务的切面 -->
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
        </aop:config>
</beans>

注意:不同的Dao层实现,使用得平台事务管理器是不一样的,体现在配置数据源,DataSource和TranscationManager类上
<!-- tx:advice的介绍 -->
<!-- 通知 事务的增强 -->
        <tx:advice id="txAdvice" transaction-manager="transcationManager">
            <!-- 事务的属性,就是事务的定义 (TranscationDefinition对象) -->
            <tx:attributes>
                <!--
                    name:对应要增强的方法
                        注意:方法名称可以与*结合使用(*表示全部省略)
                    propagation:事务的传播行为
                    isolation:事务的隔离级别
                    timeout:事务的超时时间
                    read-only:是否可读
                -->
                <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT" timeout="-1" read-only="false"

                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>

声明式事务控制的配置要点

  • 平台事务管理器

            <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"/>
            </bean>
    <!-- 注意:不同的dao层使用的事务管理器是不同的 -->
    
  • 事务的通知配置

    <!-- 通知 事务的增强 -->
            <tx:advice id="txAdvice" transaction-manager="transcationManager">
                <tx:attributes>
                    <tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT" timeout="-1" read-only="false"
    
                    <tx:method name="*"/>
                </tx:attributes>
            </tx:advice>
    
    
  • 事务的织入

     <!-- 织入 将通知与切面结合 -->
            <aop:config>
                <!-- 事务的切面 -->
                <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
            </aop:config>
    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值