java Sping aop 以及Spring aop 的应用事务管理

打印 上一主题 下一主题

主题 748|帖子 748|积分 2244

1. 回顾

线程死锁概念和如何避免死锁的发生:
线程的通信 wait  notify()  notify():---Object类
线程的状态: NEW--->start()--->就绪状态---CPU时间片---运行状态RUNNABLE]--->sleep()--->TIMED_WAITING--->wait()---->WAITING----sysn---Blocked---->终止状态[T]
线程池: 常见的线程池种类: 4种和原始
2. 正文 (3W+1H   what why where How)
  1. 1. 什么是AOP?
  2. 2. 为什么使用AOP?
  3. 3. 如何使用AOP?
  4. 4. 什么是事务?
  5. 5. spring如何实现事务管理。
复制代码
3.  什么是AOP?

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP:它是面向切面编程的语言,它可以让你的业务代码和非业务代码进行隔离。在不改变业务代码的前提下,可以增加新的非业务代码。
4. 为什么使用AOP


5. AOP应用场景


  • 记录日志
  • 权限校验
  • spring事务管理。
6. AOP的结构

AOP要做的三件事在哪里切入,也就是权限校验,等非业务操作在哪些业务 代码中执行;什么时候切入,是业务代码执行前还是执行后;切入后做什 么事,比如做权限校验、日志记录等。



  • Aspect: 切面
  • PointCut:切点:---方式: 路径表达式  (2)注解形式
  • Advice: 处理的时机。
7. 如何使用AOP

案例
  1. public class MathServiceImpl implements MathService {
  2.     public double add(double a, double b) {
  3.         double result=a+b;
  4.         System.out.println("AAA--->The add method result="+result);
  5.         return result;
  6.     }
  7.     public double mul(double a, double b) {
  8.         double result=a-b;
  9.         System.out.println("AAA--->The mul method result="+result);
  10.         return result;
  11.     }
  12.     public double cheng(double a, double b) {
  13.         double result=a*b;
  14.         System.out.println("AAA--->The cheng method result="+result);
  15.         return result;
  16.     }
  17.     public double div(double a, double b) {
  18.         double result=a/b;
  19.         System.out.println("AAA--->The div method result="+result);
  20.         return result;
  21.     }
  22. }
复制代码
发现: 我们在每个操作后,都要记录日志,如果后期日志内容发生改变。需要在每个操作后都进行修改。 不利于代码的维护。
我们来使用AOP来解决。
(1)引入相关依赖
  1. <dependencies>
  2.         
  3.         <dependency>
  4.             <groupId>org.springframework</groupId>
  5.             <artifactId>spring-webmvc</artifactId>
  6.             <version>5.2.12.RELEASE</version>
  7.         </dependency>
  8.         
  9.         <dependency>
  10.             <groupId>org.springframework</groupId>
  11.             <artifactId>spring-aspects</artifactId>
  12.             <version>5.2.12.RELEASE</version>
  13.         </dependency>
  14.     </dependencies>
复制代码
(2)创建一个切面类
  1. @Aspect //标记该类为切面类
  2. @Component //该类对象的创建交于spring容器来管理-----等价于@Service  @Controller
  3. public class MyAspect {
  4.     @Pointcut(value = "execution(public double com.ykq.aop.MathServiceImpl.add(double, double))")  //定义为切点
  5.     private void mypointcut(){}
  6.     @After(value = "mypointcut()")
  7.     public void b(){
  8.         System.out.println("AAA--->The add method result");
  9.     }
  10. }
复制代码
(3) 创建一个spring配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="com.ykq.aop"/>
  9.    
  10.     <aop:aspectj-autoproxy/>
  11. </beans>
复制代码
(4)测试
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         //加载spring配置文件
  4.         ApplicationContext app=new ClassPathXmlApplicationContext("classpath:spring.xml");
  5.         MathService mathServiceImpl = (MathService) app.getBean("mathServiceImpl");
  6.         System.out.println(mathServiceImpl.add(20, 10));
  7.     }
  8. }
复制代码
使用通配符来统配类路径
  1. @Aspect //标记该类为切面类
  2. @Component //该类对象的创建交于spring容器来管理-----等价于@Service  @Controller
  3. public class MyAspect {
  4.     //通配符:
  5.     /**
  6.      * 第一个* : 表示任意修饰符 任意返回类型。
  7.      * 第二个* : 表示该包下所有的类。
  8.      * 第三个* : 类下所有的方法
  9.      * ..: 表示任意参数
  10.      *
  11.      * 建议包就别使用统配符
  12.      */
  13.     @Pointcut(value = "execution(* com.ykq.aop.*.*(..))")  //定义为切点
  14.     private void mypointcut(){}
  15.     @After(value = "mypointcut()")
  16.     public void b(){
  17.         System.out.println("AAA--->The add method result");
  18.     }
  19. }
复制代码
7.2 注解模式

(1)自定义注解
  1. @Target(value = {ElementType.METHOD,ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface MyAnnotation {
  4.     String value() default "";
  5. }
复制代码
(2)修改切面类
  1.     @Pointcut(value = "@annotation(com.ykq.aop.MyAnnotation)")  //定义为切点
  2.     private void mypointcut2(){}
  3.     //在使用MyAnntation注解的方法之后执行内容
  4.     @After(value = "mypointcut2()")
  5.     public void b(){
  6.         System.out.println("AAA--->The add method result");
  7.     }
复制代码
7.3 aop切面通知的类型

  1. package com.aaa.aop;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.*;
  4. import org.springframework.stereotype.Component;
  5. @Aspect //标记该类为切面类
  6. @Component //该类对象的创建交于spring容器来管理-----等价于@Service  @Controller
  7. public class MyAspect {
  8.     //通配符:
  9.     /**
  10.      * 第一个* : 表示任意修饰符 任意返回类型。
  11.      * 第二个* : 表示该包下所有的类。
  12.      * 第三个* : 类下所有的方法
  13.      * ..: 表示任意参数
  14.      *
  15.      * 建议包就别使用统配符
  16.      */
  17.     @Pointcut(value = "execution(* com.ykq.aop.*.*(..))")  //定义为切点
  18.     private void mypointcut(){}
  19.     @Pointcut(value = "@annotation(com.ykq.aop.MyAnnotation)")  //定义为切点
  20.     private void mypointcut2(){}
  21. //    //在使用MyAnntation注解的方法之后执行内容。无论如何都执行。
  22. //    @After(value = "mypointcut()")
  23. //    public void a(){
  24. //        System.out.println("AAA--->The add method result");
  25. //    }
  26. //
  27. //    //前置通知:
  28. //    @Before(value = "mypointcut()")
  29. //    public void b(){
  30. //        System.out.println("========方法执行前执行切面的内容  前置通知===========");
  31. //    }
  32. //
  33. //    //后置返回通知. 碰到return. 如果方法出现异常;这种通知不会被执行
  34. //    @AfterReturning(value = "mypointcut()",returning = "r")  //returnning它会把方法执行的结果赋值给该变量
  35. //    public void afterReturning(Object r){ //参数名必须和returning的名称一致
  36. //        System.out.println("~~~~~~~~~~~~~~~~~~后置返回通知~~~~~~~~~~~~~~~~"+r);
  37. //    }
  38. //
  39. //    //异常通知: 当被切入的方法出现异常时,才会执行
  40. //    @AfterThrowing(value = "mypointcut()")
  41. //    public void afterThrowable(){
  42. //        System.out.println("==============异常通知===========================");
  43. //    }
  44.     //环绕通知。
  45.     @Around(value = "mypointcut()")
  46.     public Object around(ProceedingJoinPoint joinPoint){//joinPoint:连接点  理解为被执行的方法对象
  47.         System.out.println("业务代码执行前执行的内容======================");
  48.         try {
  49.             Object result = joinPoint.proceed();//执行你的连接点
  50.             System.out.println("方法执行完毕后~~~~~~~~~~~~~~~~~");
  51.             return result;
  52.         } catch (Throwable throwable) {
  53.             throwable.printStackTrace();
  54.             System.out.println("方法出现异常时执行~~~~~~~~~~~~~");
  55.         }finally{
  56.             System.out.println("无论如何都会执行");
  57.         }
  58.         return 0.0;
  59.     }
  60. }
复制代码
@Before 前置通知. 被代理的方法执行前--执行
@After: 后置通知: 被代理的方法执行完后--执行
@AfterReturning: 后置返回通知: 被代理的方法碰到return.--才会执行
@AfterThrowing: 后置异常通知: 当被代理的方法出现异常时--才会执行。
@Around: 环绕通知。
8. spring如何操作事务

8.1 什么是事务?

事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用.
例子: 转账
扣钱和加钱----要么都执行要么都不执行。
JDBC----它模式事务自动提交的。
  1. public class Test {    public static void main(String[] args) {        Connection conn=null;        try {            Class.forName("com.mysql.cj.jdbc.Driver");            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/aaa?serverTimezone=Asia/Shanghai","root","root");            conn.setAutoCommit(false);//设置事务手动提交。            PreparedStatement ps=conn.prepareStatement("update t_user set balance=balance-600 where id=7");            ps.executeUpdate();            //int i=10/0;            PreparedStatement ps2=conn.prepareStatement("update t_user set balance=balance+600 where id=6");            ps2.executeUpdate();            conn.commit();//事务提交        }catch (Exception e){            e.printStackTrace();            //事务回滚            try {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="com.ykq.aop"/>
  9.    
  10.     <aop:aspectj-autoproxy/>
  11. </beans>conn.rollback();            } catch (SQLException throwables) {<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xmlns:context="http://www.springframework.org/schema/context"
  15.        xmlns:aop="http://www.springframework.org/schema/aop"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.     <context:component-scan base-package="com.ykq.aop"/>
  19.    
  20.     <aop:aspectj-autoproxy/>
  21. </beans>throwables.printStackTrace();            }        }finally {        }    }}
复制代码
8.2 spring如何实现事务

spring框架一定会提供一个事务切面类。【1】前置通知---开启手动事务 [2]后置返回通知[事务提交]   [3]异常通知[事务回滚]
(1)依赖
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="com.ykq.aop"/>
  9.    
  10.     <aop:aspectj-autoproxy/>
  11. </beans>        org.springframework            spring-webmvc            5.2.12.RELEASE<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xmlns:context="http://www.springframework.org/schema/context"
  15.        xmlns:aop="http://www.springframework.org/schema/aop"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.     <context:component-scan base-package="com.ykq.aop"/>
  19.    
  20.     <aop:aspectj-autoproxy/>
  21. </beans>            mysql            mysql-connector-java            8.0.30<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xmlns:context="http://www.springframework.org/schema/context"
  25.        xmlns:aop="http://www.springframework.org/schema/aop"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  27.    
  28.     <context:component-scan base-package="com.ykq.aop"/>
  29.    
  30.     <aop:aspectj-autoproxy/>
  31. </beans>            org.springframework            spring-aspects            5.2.12.RELEASE<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xmlns:context="http://www.springframework.org/schema/context"
  35.        xmlns:aop="http://www.springframework.org/schema/aop"
  36.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  37.    
  38.     <context:component-scan base-package="com.ykq.aop"/>
  39.    
  40.     <aop:aspectj-autoproxy/>
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.        xmlns:context="http://www.springframework.org/schema/context"
  45.        xmlns:aop="http://www.springframework.org/schema/aop"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  47.    
  48.     <context:component-scan base-package="com.ykq.aop"/>
  49.    
  50.     <aop:aspectj-autoproxy/>
  51. </beans>    org.springframework            spring-tx            5.2.12.RELEASE<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.        xmlns:context="http://www.springframework.org/schema/context"
  55.        xmlns:aop="http://www.springframework.org/schema/aop"
  56.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  57.    
  58.     <context:component-scan base-package="com.ykq.aop"/>
  59.    
  60.     <aop:aspectj-autoproxy/>
  61. </beans>            org.springframework            spring-jdbc            5.2.15.RELEASE<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xmlns:context="http://www.springframework.org/schema/context"
  65.        xmlns:aop="http://www.springframework.org/schema/aop"
  66.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  67.    
  68.     <context:component-scan base-package="com.ykq.aop"/>
  69.    
  70.     <aop:aspectj-autoproxy/>
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.        xmlns:context="http://www.springframework.org/schema/context"
  75.        xmlns:aop="http://www.springframework.org/schema/aop"
  76.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  77.    
  78.     <context:component-scan base-package="com.ykq.aop"/>
  79.    
  80.     <aop:aspectj-autoproxy/>
  81. </beans>    org.mybatis            mybatis            3.5.9<?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.        xmlns:context="http://www.springframework.org/schema/context"
  85.        xmlns:aop="http://www.springframework.org/schema/aop"
  86.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  87.    
  88.     <context:component-scan base-package="com.ykq.aop"/>
  89.    
  90.     <aop:aspectj-autoproxy/>
  91. </beans><?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.        xmlns:context="http://www.springframework.org/schema/context"
  95.        xmlns:aop="http://www.springframework.org/schema/aop"
  96.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  97.    
  98.     <context:component-scan base-package="com.ykq.aop"/>
  99.    
  100.     <aop:aspectj-autoproxy/>
  101. </beans>    org.mybatis            mybatis-spring            2.0.7<?xml version="1.0" encoding="UTF-8"?>
  102. <beans xmlns="http://www.springframework.org/schema/beans"
  103.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104.        xmlns:context="http://www.springframework.org/schema/context"
  105.        xmlns:aop="http://www.springframework.org/schema/aop"
  106.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  107.    
  108.     <context:component-scan base-package="com.ykq.aop"/>
  109.    
  110.     <aop:aspectj-autoproxy/>
  111. </beans><?xml version="1.0" encoding="UTF-8"?>
  112. <beans xmlns="http://www.springframework.org/schema/beans"
  113.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  114.        xmlns:context="http://www.springframework.org/schema/context"
  115.        xmlns:aop="http://www.springframework.org/schema/aop"
  116.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  117.    
  118.     <context:component-scan base-package="com.ykq.aop"/>
  119.    
  120.     <aop:aspectj-autoproxy/>
  121. </beans>    com.alibaba            druid            1.2.8<?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.        xmlns:context="http://www.springframework.org/schema/context"
  125.        xmlns:aop="http://www.springframework.org/schema/aop"
  126.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  127.    
  128.     <context:component-scan base-package="com.ykq.aop"/>
  129.    
  130.     <aop:aspectj-autoproxy/>
  131. </beans><?xml version="1.0" encoding="UTF-8"?>
  132. <beans xmlns="http://www.springframework.org/schema/beans"
  133.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  134.        xmlns:context="http://www.springframework.org/schema/context"
  135.        xmlns:aop="http://www.springframework.org/schema/aop"
  136.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  137.    
  138.     <context:component-scan base-package="com.ykq.aop"/>
  139.    
  140.     <aop:aspectj-autoproxy/>
  141. </beans>    org.projectlombok            lombok            1.18.28            
复制代码
(2)spring配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  7.    
  8.     <context:component-scan base-package="com.ykq.aop"/>
  9.    
  10.     <aop:aspectj-autoproxy/>
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xmlns:context="http://www.springframework.org/schema/context"
  15.        xmlns:aop="http://www.springframework.org/schema/aop"
  16.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  17.    
  18.     <context:component-scan base-package="com.ykq.aop"/>
  19.    
  20.     <aop:aspectj-autoproxy/>
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.        xmlns:context="http://www.springframework.org/schema/context"
  25.        xmlns:aop="http://www.springframework.org/schema/aop"
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  27.    
  28.     <context:component-scan base-package="com.ykq.aop"/>
  29.    
  30.     <aop:aspectj-autoproxy/>
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xmlns:context="http://www.springframework.org/schema/context"
  35.        xmlns:aop="http://www.springframework.org/schema/aop"
  36.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  37.    
  38.     <context:component-scan base-package="com.ykq.aop"/>
  39.    
  40.     <aop:aspectj-autoproxy/>
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.        xmlns:context="http://www.springframework.org/schema/context"
  45.        xmlns:aop="http://www.springframework.org/schema/aop"
  46.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  47.    
  48.     <context:component-scan base-package="com.ykq.aop"/>
  49.    
  50.     <aop:aspectj-autoproxy/>
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.        xmlns:context="http://www.springframework.org/schema/context"
  55.        xmlns:aop="http://www.springframework.org/schema/aop"
  56.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  57.    
  58.     <context:component-scan base-package="com.ykq.aop"/>
  59.    
  60.     <aop:aspectj-autoproxy/>
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.        xmlns:context="http://www.springframework.org/schema/context"
  65.        xmlns:aop="http://www.springframework.org/schema/aop"
  66.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  67.    
  68.     <context:component-scan base-package="com.ykq.aop"/>
  69.    
  70.     <aop:aspectj-autoproxy/>
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.        xmlns:context="http://www.springframework.org/schema/context"
  75.        xmlns:aop="http://www.springframework.org/schema/aop"
  76.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  77.    
  78.     <context:component-scan base-package="com.ykq.aop"/>
  79.    
  80.     <aop:aspectj-autoproxy/>
  81. </beans><?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.        xmlns:context="http://www.springframework.org/schema/context"
  85.        xmlns:aop="http://www.springframework.org/schema/aop"
  86.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  87.    
  88.     <context:component-scan base-package="com.ykq.aop"/>
  89.    
  90.     <aop:aspectj-autoproxy/>
  91. </beans><?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.        xmlns:context="http://www.springframework.org/schema/context"
  95.        xmlns:aop="http://www.springframework.org/schema/aop"
  96.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  97.    
  98.     <context:component-scan base-package="com.ykq.aop"/>
  99.    
  100.     <aop:aspectj-autoproxy/>
  101. </beans>         
复制代码
(3) dao类和xml
  1. public interface UserDao {
  2.     //1.修改账号余额
  3.     public void updateBalance(@Param("id") int id, @Param("money") double money);
  4. }
复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.ddd.dao.UserDao">
  5.     <update id="updateBalance">
  6.           update  t_user set balance=balance+#{money} where id=#{id}
  7.     </update>
  8. </mapper>
复制代码
(4)service
  1. package com.ddd.service.impl;
  2. import com.ddd.dao.UserDao;
  3. import com.ddd.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. @Service
  8. public class UserServieImpl implements UserService {
  9.     @Autowired
  10.     private UserDao userDao;
  11.     @Transactional //该方法交于spring的事务来管理了---默认spring不识别该注解
  12.     public void zhuanzhang(int id, int uid, double money) {
  13.         //1.扣钱
  14.         userDao.updateBalance(id,-money);
  15.         //int c=10/0;
  16.         //2.收钱
  17.         userDao.updateBalance(uid,money);
  18.     }
  19. }
复制代码
(5)测试:
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         ApplicationContext app=new ClassPathXmlApplicationContext("classpath:spring.xml");
  4.         UserService userServieImpl = (UserService) app.getBean("userServieImpl");
  5.         userServieImpl.zhuanzhang(7,6,400);
  6.     }
  7. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表