1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”)
2.步骤二:引入AOP的开发包
3.步骤三:引入applicationContext.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:jdbc="http://www.springframework.org/schema/jdbc"
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/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"> </beans>

   * 管理C3P0连接池

     * 先引入C3P0的jar包
* com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar * 编写配置文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
4.
步骤四:创建对应的包结构和类(具体内容见https://www.cnblogs.com/wyhluckdog/p/10137283.html
    * com.huida.demo1
* AccountService
* AccountServlceImpl
* AccountDao
* AccountDaoImpl
5.步骤五:
引入Spring的配置文件,将类配置到Spring中
<bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
6.步骤六:配置事务管理器
  <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
7.步骤七:配置事务增强
 <!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
name :绑定事务的方法名,可以使用通配符,可以配置多个。
propagation :传播行为
isolation :隔离级别
read-only :是否只读
timeout :超时信息
rollback-for:发生哪些异常回滚.
no-rollback-for:发生哪些异常不回滚.
-->
<!-- 哪些方法加事务 -->
<tx:method name="pay" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
8.步骤八:书写切面类MyAdvice:
package com.huida.demo1;

public class MyAdvice {

    public void log(){
System.out.println("添加日志");
} }

9.步骤九:配置AOP的切面

<bean id="myAdvice" class="com.huida.demo1.MyAdvice"></bean>
<aop:config>
<aop:aspect ref="myAdvice">
<aop:before method="log" pointcut="execution(* com.huida.demo1.AccountServiceImpl.pay(..))"/>
</aop:aspect>
</aop:config>

10.完整的配置文件的配置信息为:

<?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:jdbc="http://www.springframework.org/schema/jdbc"
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/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="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="pay" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <bean id="myAdvice" class="com.huida.demo1.MyAdvice"></bean> <aop:config>
<aop:aspect ref="myAdvice">
<aop:before method="log" pointcut="execution(* com.huida.demo1.AccountServiceImpl.pay(..))"/>
</aop:aspect>
</aop:config> <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
<!-- <property name="transactionTemplate" ref="transactionTemplate"/> -->
</bean> </beans>

11.步骤十:编写测试类

package com.huida.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 { @Resource(name="accountService")
private AccountService accountService; @Test
public void run1(){
accountService.pay("小明","小红",1000);
}
}

12.单元测试run1方法,刷新spring-day03数据库中的user表,可以看到小明的money减少了1000,而小红的money增加了1000.



Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)的更多相关文章

  1. Spring框架的事务管理之基于AspectJ的注解方式(重点掌握,最简单的方式)

    1. 步骤一:恢复转账的开发环境(具体开发环境实现见:https://www.cnblogs.com/wyhluckdog/p/10137283.html)2. 步骤二:applicationCont ...

  2. Spring 框架的事务管理

    1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接 ...

  3. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  4. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  5. Spring框架的事务管理之声明式事务管理的类型

    1. 声明式事务管理又分成两种方式 * 基于AspectJ的XML方式(重点掌握)(具体内容见“https://www.cnblogs.com/wyhluckdog/p/10137712.html”) ...

  6. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

  7. Spring框架之事务管理

    Spring框架之事务管理 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 一致性:指事务前后 ...

  8. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  9. 基于AspectJ的XML方式进行AOP开发

    -------------------siwuxie095                                 基于 AspectJ 的 XML 方式进行 AOP 开发         1 ...

随机推荐

  1. Python中的logger和handler到底是个什么鬼

    最近的任务经常涉及到日志的记录,特意去又学了一遍logging的记录方法.跟java一样,python的日志记录也是比较繁琐的一件事,在写一条记录之前,要写好多东西.典型的日志记录的步骤是这样的: 创 ...

  2. 你的GAN训练得如何--GAN 的召回率(多样性)和精确率(图像质量)方法评估

    生成对抗网络(GAN)是当今最流行的图像生成方法之一,但评估和比较 GAN 产生的图像却极具挑战性.之前许多针对 GAN 合成图像的研究都只用了主观视觉评估,一些定量标准直到最近才开始出现.本文认为现 ...

  3. IntelliJ IDEA神器使用技巧笔记

    1. Alt + 数字 打开idea 快捷键打开相应的窗口: 高效定位代码: 无处不在的跳转 1.项目间的跳转: Windows ->   ctrl+alt+[   /  ] 2.文件之间的跳转 ...

  4. js的sort(0实现数组的排序

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. Zookeeper 介绍翻译

    源网址链接 https://zookeeper.apache.org/ Apache Zookeeper 开放源码的服务器,提供高可靠的分布式协调服务. Zookeeper是一个维护配置信息,命名服务 ...

  6. Mybatis 为什么不要用二级缓存

    https://www.cnblogs.com/liouwei4083/p/6025929.html mybatis 二级缓存不推荐使用 一 mybatis的缓存使用. 大体就是首先根据你的sqlid ...

  7. Yii-CHtmlPurifier- 净化器的使用(yii过滤不良代码)

    1. 在控制器中使用: public function actionCreate() { $model=new News; $purifier = new CHtmlPurifier(); $puri ...

  8. cacti客户端snmp设置

    1. ubuntu : apt-get install snmp snmpd vim /etc/default/snmpd  //将此配置文件中127.0.0.1 删掉. /etc/init.d/sn ...

  9. Game2048

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. 一行转多行 及多行转一行的 hive语句

      注意 :|,: 是特殊符号,要用 "\\|", "\\;"来表示.   一行转多行 usertags 里面有很多项,每项之间以逗号分隔   create t ...