1. 程式人生 > >SpringBoot自定義註解Annotation的使用

SpringBoot自定義註解Annotation的使用

 

 

 

一. 首先匯入相關包, 在build.gradle中新增

dependencies {
    //支援AOP
    compile('org.springframework.boot:spring-boot-starter-aop')
}

 


二. 新增一個自定義的註解類OperateLogAnnotation:

import java.lang.annotation.*;

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperateLogAnnotation {
    String value();
}


三. 然後建立Aspect測試類 TestAspect:

import com.great.annotation.OperateLogAnnotation;
import com.great.annotation.TestAnnotation;
//import javassist.bytecode.SignatureAttribute;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
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.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect // FOR AOP
@Order(-99) // 控制多個Aspect的執行順序,越小越先執行, 當然也可以不寫這注解, 對於寫和不寫@order的兩個切面, 有@order的優先於無@order的執行; 都有@order時, 越小越執先執行
@Component
public class TestAspect {

    // @Before可以有兩者寫法, @annotation(形參test)
    @Before("@annotation(test)")// 攔截被TestAnnotation註解的方法;如果你需要攔截指定package指定規則名稱的方法,可以使用表示式execution(...),具體百度一下資料一大堆
    public void beforeTest(JoinPoint point, TestAnnotation test) throws Throwable {
        System.out.println("beforeTest:" + test.name());
    }

    @After("@annotation(test)")
    public void afterTest(JoinPoint point, TestAnnotation test) {
        System.out.println("afterTest:" + test.name());
    }

/* 
   // @Before可以有兩者寫法, @annotation(函式名annotationPointCut)
   @Before("annotationPointCut()")
    public void before(JoinPoint joinPoint) {
        MethodSignature sign = (MethodSignature) joinPoint.getSignature();
        Method method = sign.getMethod();
        OperateLogAnnotation annotation = method.getAnnotation(OperateLogAnnotation.class);
        System.out.println("列印:" + annotation.value() + " 前置日誌1");
    }

   // 指定切面
   @Pointcut("@annotation(com.great.annotation.OperateLogAnnotation)")
    public void annotationPointCut() {
    }

    @After("annotationPointCut()")
    public void afterTTT(JoinPoint point) {
        MethodSignature sign = (MethodSignature) point.getSignature();
        Method method = sign.getMethod();
        OperateLogAnnotation annotation = method.getAnnotation(OperateLogAnnotation.class);
        System.out.println("列印自帶引數:" + annotation.value() + " 後置日誌1");
    }
*/

}

 


然後建立一個TestAOPController 驗證一下:

import com.great.annotation.OperateLogAnnotation;
import com.great.annotation.TestAnnotation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestAOPController {


    @RequestMapping("/show3")
    @ResponseBody
    @OperateLogAnnotation("測試")   // 加上測試註解
    public String getById() {
        return "hello";
    }

}