5

Spring Boot AOP 获取注解中的值

 3 years ago
source link: https://www.bugcatt.com/archives/2673
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

阿航

2021年5月12日

2

Spring Boot AOP 获取注解中的值

首先来确定一下我们的需求:

我需要做一个功能, 可以通过注解的方式来使某些controller做一些事情(比如权限校验).

  • java 8
  • Spring Boot

首先来创建一个注解类MyAnnotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

    /**
     * 注解值(等下要获取并处理该值)
     * @return
     */
    String value() default "";

}

还有处理注解的类MyAnnotaionAspect:

注意, 需要将下方的@annotation内的值替换为你的注解路径!
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class MyAnnotationAspect {

    protected static final Logger logger = LoggerFactory.getLogger(MyAnnotationAspect.class);

    // TODO 注意这里需要替换为你的注解路径
    @Around("@annotation(com.example.demo.MyAnnotation)")
    public Object doSomething(ProceedingJoinPoint point) throws Throwable {

        MethodSignature signature = (MethodSignature) point.getSignature();
        Method signatureMethod = signature.getMethod();

        MyAnnotation myAnnotation = signatureMethod.getAnnotation(MyAnnotation.class);

        String value = myAnnotation.value();

        // 这里就可以打印你的注解值了
        System.out.println(value);

        return point.proceed();
    }

}

写一个Controller作为例子:

import com.example.demo.aop2.aop.MyAnnotation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    // 这里添加我们定义的注解
    @MyAnnotation(value = "123456")
    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public @ResponseBody
    String demo() {
        return "hello";
    }

}

运行Spring Boot, 尝试调用该接口, 然后看看控制台是否成功打印了你传入的value: 123456


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK