美文网首页
自定义注解

自定义注解

作者: Ktry | 来源:发表于2021-07-14 17:00 被阅读0次

注解类


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 "系统错误!";
        }

    }

}

相关文章

网友评论

      本文标题:自定义注解

      本文链接:https://www.haomeiwen.com/subject/vpwypltx.html