日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

AOP切面編程,以及自定義注解實現切面

作者:Bunny0212 更新時間: 2024-03-14 編程語言

AOP切面編程

  1. 通知類型
  2. 表達式
  3. 重用表達式
  4. 切面優先級
  5. 使用注解開發,加上注解實現某些功能

簡介

  • 動態代理分為JDK動態代理和cglib動態代理
  • 當目標類有接口的情況使用JDK動態代理和cglib動態代理,沒有接口時只能使用cglib動態代理
  • JDK動態代理動態生成的代理類會在com.sun.proxy包下,類名為$proxy1,和目標類實現相同的接口
  • cglib動態代理動態生成的代理類會和目標在在相同的包下,會繼承目標類
  • 動態代理(InvocationHandler):JDK原生的實現方式,需要被代理的目標類必須實現接口。因為這個技術要求代理對象和目標對象實現同樣的接口(兄弟兩個拜把子模式)。
  • cglib:通過繼承被代理的目標類(認干爹模式)實現代理,所以不需要目標類實現接口。
  • AspectJ:是AOP思想的一種實現。本質上是靜態代理,將代理邏輯“織入”被代理的目標類編譯得到的字節碼文件,所以最終效果是動態的。weaver就是織入器。Spring只是借用了AspectJ中的注解。

依賴

<!--spring context依賴-->
<!--當你引入Spring Context依賴之后,表示將Spring的基礎依賴引入了-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.0.2</version>
</dependency>

<!--spring aop依賴-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>6.0.2</version>
</dependency>
<!--spring aspects依賴-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>6.0.2</version>
</dependency>

注解說明

  • @Aspect表示這個類是一個切面類
  • @Component注解保證這個切面類能夠放入IOC容器

簡單使用

幾種通知類型

  • 前置通知@Before()
  • 環繞通知@Around()
  • 返回通知@AfterReturning()
  • 異常通知@AfterThrowing()
  • 后置通知@After()

表達式介紹

在這里插入圖片描述

切入點表達式

在方法中可以傳入參數public void afterMethod(JoinPoint joinPoint),類型為JoinPoint

  • 獲取連接點的簽名信息

    • String methodName = joinPoint.getSignature().getName()
      
  • 獲取目標方法的返回值

    • 其中表達式中returning后面參數result要與public void afterReturningMethod(JoinPoint joinPoint, Object result)傳入參數名稱一樣,類型可以不一樣但是名稱要一樣。

    • @AfterReturning(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))", returning = "result")
      public void afterReturningMethod(JoinPoint joinPoint, Object result){
          String methodName = joinPoint.getSignature().getName();
          System.out.println("Logger-->返回通知,方法名:"+methodName+",結果:"+result);
      }
      
  • 獲取目標方法的異常

    • 在表達式中加入throwing = "ex",和上面一樣,傳入參數名稱要一直,也要是ex,如:Throwable ex

    • @AfterThrowing(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))", throwing = "ex")
      public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
          String methodName = joinPoint.getSignature().getName();
          System.out.println("Logger-->異常通知,方法名:"+methodName+",異常:"+ex);
      }
      
切入點表達式使用

前置通知

@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
    // 設置切入點和通知類型
    @Before(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))")
    public void beforeMethod(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("Logger-->前置通知" + "參數:" + args[0] + "," + args[1]);
    }
}

后置通知

@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
    @After(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))")
    public void afterMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();// 方法名

        System.out.println("Logger-->后置通知,方法名:" + name);
    }
}

返回通知

@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
    @AfterReturning(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))", returning = "result")
    public void afterReturn(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->返回通知,方法名:" + methodName + ",結果:" + result);
    }
}

異常通知

@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
    @AfterThrowing(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))", throwing = "ex")
    public void afterThrowingMethod(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Logger-->異常通知,方法名:" + methodName + ",異常:" + ex);
    }
}

環繞通知

@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
    @Around(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        Object result = null;
        try {
            System.out.println("環繞通知-->目標對象方法執行之前");
            // 目標對象(連接點)方法的執行
            result = joinPoint.proceed();
            System.out.println("環繞通知-->目標對象方法返回值之后");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("環繞通知-->目標對象方法出現異常時");
        } finally {
            System.out.println("環繞通知-->目標對象方法執行完畢");
        }
        return result;
    }
}
重用切入點表達式

申明表達式

@Pointcut("execution(* com.atguigu.aop.annotation.*.*(..))")
public void pointCut(){}

在方法中使用

@Before("pointCut()")
public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs());
    System.out.println("Logger-->前置通知,方法名:"+methodName+",參數:"+args);
}

在不同切面中使用

@Before("com.atguigu.aop.CommonPointCut.pointCut()")
public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs());
    System.out.println("Logger-->前置通知,方法名:"+methodName+",參數:"+args);
}

在這里插入圖片描述

切面的優先級

相同目標方法上同時存在多個切面時,切面的優先級控制切面的內外嵌套順序。

  • 優先級高的切面:外面
  • 優先級低的切面:里面

使用@Order注解可以控制切面的優先級:

  • @Order(較小的數):優先級高
  • @Order(較大的數):優先級低

將@Order注解放在切面類上而不是方法上!!!

  • 優先級低的
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
@Order(4)
public class LogAspect {
    @After(value = "pointcut()")
    public void afterMethod2(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();// 方法名

        System.out.println("Logger-->后置通知 111111,方法名:" + name);
    }
}
  • 優先級高的
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
@Order(1)
public class NewLogAspect {
    @After(value = "com.example.aop.annoaop.LogAspect.pointcut()")
    public void afterMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();// 方法名

        System.out.println("Logger-->后置通知 444444,方法名:" + name);
    }
}
效果

原先交換順序后

在這里插入圖片描述

沒有交換順序前

在這里插入圖片描述

注解使用

使用AOP在方法或者接口上寫上某些注解,完成特定方法。

實現思路

  1. 創建@interface
  2. 和上面一樣要寫Aspect,并且要被spring管理
  3. 在方法或者接口上加上@interface注解

實現示例

  • 創建@interface 并命名為BunnyLog
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
// 加上這個注解打印當前時間
public @interface BunnyLog {
    
}
  • 使用AOP切面,需要注意的是:

    • 如果只是單純的加上注解,不考慮指定類或者類中方法,完成某些功能,要修改下面切入點

      • @Pointcut(value = "@annotation(com.example.aop.annoaop.BunnyLog)")
        public void bunnyPointcut() {
        }
        
    • 如果想指定某些類或者某些方法下的

      • @Pointcut("execution(* com.example.aop.annoaop.*.*(..)) && @annotation(com.example.aop.annoaop.BunnyLog)")
        public void bunnyPointcut() {
        }
        
  • 創建切面

@Aspect
@Component
public class BunnyAspect {
    /**
     * 切入點,并且加上了 @BunnyLog注解
     */
    @Pointcut(value = "execution(* com.example.aop.annoaop.*.*(..)) || @annotation(com.example.aop.annoaop.BunnyLog)")
    public void bunnyPointcut() {
    }

    @Before("bunnyPointcut()")
    public void before(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("------AOP前置通知生效,方法名稱------>" + name);

        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyy年MM月dd日 HH:mm:ss");
        String format = localDateTime.format(timeFormatter);
        System.out.println("BunnyAspect,現在時間====>" + format);
    }
}
  • 普通方法,需要在類上加上@Component被spring管理,之后再方法中加上注解@BunnyLog
@Component
public class BunnyTestImpl {
    @BunnyLog
    public void method() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyy年MM月dd日 HH:mm:ss");
        String format = localDateTime.format(timeFormatter);
        System.out.println("測試方法,現在時間====>" + format);
    }
}
  • 創建一個測試類
public class TestBefore {
    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        BunnyTestImpl bean = context.getBean(BunnyTestImpl.class);
        bean.method();
    }
}

執行結果:

r);
System.out.println(“測試方法,現在時間====>” + format);
}
}


- 創建一個測試類

```java
public class TestBefore {
    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        BunnyTestImpl bean = context.getBean(BunnyTestImpl.class);
        bean.method();
    }
}

執行結果:

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/weixin_46533577/article/details/136605066

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新