1. 程式人生 > >SpringBoot —— AOP註解式攔截與方法規則攔截

SpringBoot —— AOP註解式攔截與方法規則攔截

運行 處理程序 return 編譯 clear 字節碼 動態獲取 nconf {}

  AspectJ是一個面向切面的框架,它擴展了Java語言。AspectJ定義了AOP語法,所以它有一個專門的編譯器用來生成遵守Java字節編碼規範的Class文件。

  SpringBoot中AOP的使用方式主要有兩種:註解式攔截與方法規則攔截,具體使用如下文所示。

一、創建一個簡單springboot 2.03項目,添加aop依賴

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

 此依賴已包含AspectJ相關依賴包。

二、編寫攔截規則的註解

package com.cenobitor.aop.annotation;

import java.lang.annotation.*;

@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented
public @interface Action {
    String name();
}

註解說明:元註解是指註解的註解,包括@Retention @Target @Document @Inherited四種。

1.@Retention: 定義註解的保留策略

  • @Retention(RetentionPolicy.SOURCE) //註解僅存在於源碼中,在class字節碼文件中不包含
  • @Retention(RetentionPolicy.CLASS) // 默認的保留策略,註解會在class字節碼文件中存在,但運行時無法獲得,
  • @Retention(RetentionPolicy.RUNTIME) // 註解會在class字節碼文件中存在,在運行時可以通過反射獲取到

  首先要明確生命周期長度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方後者一定也能作用。一般如果需要在運行時去動態獲取註解信息,那只能用 RUNTIME 註解;如果要在編譯時進行一些預處理操作,比如生成一些輔助代碼(如 ButterKnife),就用 CLASS註解;如果只是做一些檢查性的操作,比如 @Override 和 @SuppressWarnings,則可選用 SOURCE 註解。

2.@Target:定義註解的作用目標
 源碼為:

@Documented  
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.ANNOTATION_TYPE)  
public @interface Target {  
    ElementType[] value();  
}  
  • @Target(ElementType.TYPE) //接口、類、枚舉、註解
  • @Target(ElementType.FIELD) //字段、枚舉的常量
  • @Target(ElementType.METHOD) //方法
  • @Target(ElementType.PARAMETER) //方法參數
  • @Target(ElementType.CONSTRUCTOR) //構造函數
  • @Target(ElementType.LOCAL_VARIABLE)//局部變量
  • @Target(ElementType.ANNOTATION_TYPE)//註解
  • @Target(ElementType.PACKAGE) ///包

3.@Document:說明該註解將被包含在javadoc中
4.@Inherited:說明子類可以繼承父類中的該註解

三、編寫使用註解的被攔截類

package com.cenobitor.aop.service;

import com.cenobitor.aop.annotation.Action;
import org.springframework.stereotype.Service;

@Service
public class DemoAnnotationService {
    @Action(name = "註解式攔截的add操作")
    public void add(){}
}

四、編寫使用方法規則被攔截類

package com.cenobitor.aop.service;

import org.springframework.stereotype.Service;

@Service
public class DemoMethodService {
    public void add(){}
}

五、編寫切面

package com.cenobitor.aop.aspect;

import com.cenobitor.aop.annotation.Action;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(com.cenobitor.aop.annotation.Action)")
    public void annotationPoinCut(){}

    @After("annotationPoinCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("註解式攔截 "+action.name());
    }

    @Before("execution(* com.cenobitor.aop.service.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法規則式攔截,"+method.getName());
    }
}

AOP註解說明:

  • @Aspect 定義切面:切面由切點和增強(引介)組成(可以包含多個切點和多個增強),它既包括了橫切邏輯的定義,也包括了連接點的定義,SpringAOP就是負責實施切面的框架,它將切面所定義的橫切邏輯織入到切面所指定的鏈接點中。
  • @Pointcut 定義切點:切點是一組連接點的集合。AOP通過“切點”定位特定的連接點。通過數據庫查詢的概念來理解切點和連接點的關系再適合不過了:連接點相當於數據庫中的記錄,而切點相當於查詢條件。
  • @Before :在目標方法被調用之前做增強處理,@Before只需要指定切入點表達式即可。
  • @AfterReturning : 在目標方法正常完成後做增強,@AfterReturning除了指定切入點表達式後,還可以指定一個返回值形參名returning,代表目標方法的返回值。
  • @Afterthrowing: 主要用來處理程序中未處理的異常,@AfterThrowing除了指定切入點表達式後,還可以指定一個throwing的返回值形參名,可以通過該形參名來訪問目標方法中所拋出的異常對象。
  • @After: 在目標方法完成之後做增強,無論目標方法時候成功完成。@After可以指定一個切入點表達式。
  • @Around: 環繞通知,在目標方法完成前後做增強處理,環繞通知是最重要的通知類型,像事務,日誌等都是環繞通知,註意編程中核心是一個ProceedingJoinPoint。

六、運行

public class main {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopApplication.class);
        DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
        DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
        demoAnnotationService.add();
        demoMethodService.add();
    }

}

AopApplication.class為本項目的啟動類。

運行結果如下:

註解式攔截 註解式攔截的add操作
方法規則式攔截,add

註:摘抄自《JavaEE開發的顛覆者SpringBoot 實戰》,根據springboot 2.0.3做些許修改,省略一些配置項。

SpringBoot —— AOP註解式攔截與方法規則攔截