0
点赞
收藏
分享

微信扫一扫

基于注解的拦截器Interceptor

流沙雨帘 2022-02-21 阅读 28

== 步骤 ==

  1. 创建注解
// 元注解,注解的注解
@Target 标明该注解可以作用的对象,可以是类、方法、成员变量等
@Retention 保留的时间 ,可以保留到编译期,或者运行期等
@Document 对于含有该注解的类 生成文档注释
@Inherit 注解可以继承

// 实例
@Target(ElementType.Method)
@Retention(Plolicy.RUNTIME)
public @interface t{

}
  1. 把生成的注解作用到方法上

	@Requesting(path="/user",method=RequestMethod.POST)
	@t
	public void getUser(){
	}
  1. 拦截器处理含有该注解的类
class interceptor implement HandlerInterceptor{
	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            // 获取注解
            method.getAnnotation(t.class);
            //判断这个访问的路径是否满足要求
        }
        return true;
    }
}

举报

相关推荐

0 条评论