【SpringBoot】面向切面编程AOP

发布时间 2023-09-02 10:48:31作者: 编程筑基

一.基本概念

oop:主要以函数或者过程为程序的基本组织方式,系统功能是由一组相关的过程和函数序列构成,主要实现通过继承,多态和封装

aop:这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。简单来讲就是方法和方法的关联,将公用代码从核心代码中抽取出来,程序员只需要关顾核心代码的实现

OOP(面向对象编程)针对问题领域中以及业务处理过程中存在的实体及其属性和操作进行抽象和封装,面向对象的核心概念是纵向结构的,其目的是获得更加清晰高效的逻辑单元划分;

AOP则是针对业务处理过程中的切面进行提取,用面向对象的思路,将业务操作对象的核心功能和对它的其他服务性功能代码分离,即某一个操作在各个模块中都有涉及,这个操作就可以看成“横切”存在于系统当中。在许多情况下,这些操作都是与业务逻辑相关性不强或者不属于逻辑操作的必须部分,而面向对象的方法很难对这种情况做出处理。AOP可以认为是对OOP的补充,解决了OOP无法处理的横向的问题

实现方法

  1. 导入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>    

     

  2. 在项目路径下新建文件:aop->xxAspect.java

       

 

  3.demoAspect.java

 1 package com.wanmait.mpdemo.aop;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.*;
 6 
 7 import java.util.Arrays;
 8 
 9 //@Component
10 //@Aspect//切面类
11 public class DemoAspect {
12     //定义切入点表达式 对com.wanmait.mpdemo.service包下面的所有的类或者接口的所有的方法有任意的参数个数和参数类型都进行切入
13     @Pointcut("execution(* com.wanmait.mpdemo.service.*.*(..))")
14     public void pointcutExpression(){}
15 
16     //前置通知,在目标方法执行之前执行
17     @Before("pointcutExpression()")
18     public void before(JoinPoint point){
19         System.out.println("before..............");
20         System.out.println("类:"+point.getTarget().getClass().getName());
21         System.out.println("方法:"+point.getSignature().getName());
22         System.out.println("参数:"+ Arrays.toString(point.getArgs()));
23     }
24     //后置通知,不管目标方法是否执行成功都会执行
25     @After("pointcutExpression()")
26     //@After(value = "pointcutExpression()")
27     public void after(JoinPoint joinPoint){
28         System.out.println("after..................");
29     }
30     //异常通知,目标方法抛出异常时执行,后置通知的一种
31     @AfterThrowing(value = "pointcutExpression()",throwing = "ex")
32     public void afterThrowing(JoinPoint point,Exception ex){
33         System.out.println("afterThowing...........");
34         System.out.println(ex.getMessage());
35     }
36     //返回通知,目标方法正常执行时执行,后置通知的一种
37     @AfterReturning(value = "pointcutExpression()",returning = "returnValue")
38     public void afterReturning(JoinPoint joinPoint,Object returnValue){
39         System.out.println("afterReturning...........");
40         //如果目标方法没有返回值,returnValue就是null
41         System.out.println(returnValue);
42     }
43 
44     //环绕通知
45     @Around("pointcutExpression()")
46     public Object around(ProceedingJoinPoint point) throws Throwable {
47         System.out.println("around before....................");
48         long beginTime = System.currentTimeMillis();
49         //手动调用目标方法
50         Object returnValue = null;
51         returnValue = point.proceed();
52         long endTime = System.currentTimeMillis();
53         System.out.println("执行时间:"+(endTime-beginTime)+"毫秒");
54         System.out.println("around after....................");
55         return returnValue;
56     }
57 }