SpringBoot自定义注解Annotation的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guyue35/article/details/84390257

一. 首先导入相关包, 在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";
    }

}

 

猜你喜欢

转载自blog.csdn.net/guyue35/article/details/84390257