先放一下百度的关于注解的基本东西
Target:描述了注解修饰的对象范围,取值在java.lang.annotation.ElementType
定义,常用的包括:
- METHOD:用于描述方法
- PACKAGE:用于描述包
- PARAMETER:用于描述方法变量
- TYPE:用于描述类、接口或enum类型
Retention: 表示注解保留时间长短。取值在java.lang.annotation.RetentionPolicy
中,取值为:
- SOURCE:在源文件中有效,编译过程中会被忽略
- CLASS:随源文件一起编译在class文件中,运行时忽略
- RUNTIME:在运行时有效
- @Documented - 标记这些注解是否包含在用户文档中。
我用的是配合拦截器和注解实现接口放过,反着也一样。主要原理就是通过反射获取是否包含注解。
package com.base.interceptor; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Created by Administrator on 2021/7/1. * 自定义注解 当拦截器识别到注解时 就跳过 */ @Target(value = {ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface IgnoreInterceptor { }
然后去拦截器
//添加注解的过滤掉 if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; IgnoreInterceptor methodAnnotation = handlerMethod.getMethodAnnotation(IgnoreInterceptor.class); if (methodAnnotation != null){ System.out.println("Method @IgnoreInterceptor"); return true; } /*Class<?> clazz = handlerMethod.getBeanType(); if (AnnotationUtils.findAnnotation(clazz, IgnoreInterceptor.class) != null) { System.out.println("Class @UnAuthRequest"); return true; }*/ }