自定义注解

2021-07-14  本文已影响0人  Ktry

注解类


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MayiktCurrentLimit {
    String name() default "";
    double token() default 20;
}

注解拦截

import com.mayikt.service.annotation.MayiktCurrentLimit;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;



@Aspect
@Component
public class CurrentLimitAop {

    @Around(value = "@annotation(com.mayikt.service.annotation.MayiktCurrentLimit)")
    public Object around(ProceedingJoinPoint joinPoint) {
        // 走该环绕通知
        try {
            //获取拦截的方法名
            Signature sig = joinPoint.getSignature();
            //获取拦截的方法名
            MethodSignature methodSignature = (MethodSignature) sig;
            MayiktCurrentLimit mayiktCurrentLimit = methodSignature.getMethod().getDeclaredAnnotation(MayiktCurrentLimit.class);
            // 获取到该注解的name
            String name = mayiktCurrentLimit.name();
            // 获取该注解的token
            double token = mayiktCurrentLimit.token();
            
            Object proceedResult = joinPoint.proceed();// 目标方法   public String get() {}
            return proceedResult;
        } catch (Throwable throwable) {
            return "系统错误!";
        }

    }

}
上一篇 下一篇

猜你喜欢

热点阅读