InterceptorRegistry 拦截器注册类登记类

InterceptorRegistry

org.springframework.web.servlet.config.annotation.InterceptorRegistry 帮助配置映射截取程序列表

属性

/*
 * 拦截器注册类集合
 */
private final List registrations = new ArrayList<>();

/*
 * 用于拦截器顺序排序规则
 * 自定义规则
 */
private static final Comparator INTERCEPTOR_ORDER_COMPARATOR =
        OrderComparator.INSTANCE.withSourceProvider(object -> {
            if (object instanceof InterceptorRegistration) {
                return (Ordered) ((InterceptorRegistration) object)::getOrder;
            }
            return null;
        }); 
  

addInterceptor方法

/**
 * 添加提供的{@link HandlerInterceptor}。
 * 拦截器用于添加@return {@link InterceptorRegistration},允许您进一步配置已注册的拦截器,
 * 例如添加它应该应用到的URL模式。
 */
public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {
    InterceptorRegistration registration = new InterceptorRegistration(interceptor);
    this.registrations.add(registration);
    return registration;
}

addWebRequestInterceptor方法

/**
 * 添加提供的{@link WebRequestInterceptor}。
 * 拦截器用于添加@return {@link InterceptorRegistration},允许您进一步配置已注册的拦截器,
 * 例如添加它应该应用到的URL模式。
 */
public InterceptorRegistration addWebRequestInterceptor(WebRequestInterceptor interceptor) {
    WebRequestHandlerInterceptorAdapter adapted = new WebRequestHandlerInterceptorAdapter(interceptor);
    InterceptorRegistration registration = new InterceptorRegistration(adapted);
    this.registrations.add(registration);
    return registration;
}

getInterceptors方法

/**
 * 返回所有已注册的拦截器。
 */
protected List getInterceptors() {
    return this.registrations.stream()
        .sorted(INTERCEPTOR_ORDER_COMPARATOR)
        .map(InterceptorRegistration::getInterceptor)
        .collect(Collectors.toList());
} 
  

 

你可能感兴趣的:(Java,spring)