View Javadoc
1   package top.infra.web.servlet.handler;
2   
3   import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
4   import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
5   
6   import java.util.Comparator;
7   import java.util.List;
8   
9   /**
10   * Created by zhuowan on 2018/5/15 15:28.
11   * Description:
12   */
13  public abstract class OrderedHandlerInterceptorWebMvcConfigureAdapter extends WebMvcConfigurerAdapter {
14  
15      private static Comparator<OrderedHandlerInterceptor<?>> comparator() {
16          return Comparator.comparingInt(i -> i.getInterceptor().getOrder());
17      }
18  
19      public abstract List<OrderedHandlerInterceptor<?>> addOrderedInterceptors();
20  
21      @Override
22      public void addInterceptors(final InterceptorRegistry registry) {
23          final List<OrderedHandlerInterceptor<?>> interceptors = this.addOrderedInterceptors();
24          interceptors
25              .stream()
26              .sorted(comparator())
27              .forEach(i -> registry.addInterceptor(i.getInterceptor()).addPathPatterns(i.getPatterns()));
28      }
29  }