Spring之Ordered

在SpringMVC之HandlerMapping中,AbstractHandlerMapping实现了Ordered接口,那么Ordered接口的作用是什么呢?接下来,我们看一下Ordered接口。

Ordered接口

接口定义

public interface Ordered {
    int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
    int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
    int getOrder();
}

//OrderComparator类compare()
public int compare(Object o1, Object o2) {
  boolean p1 = (o1 instanceof PriorityOrdered);
  boolean p2 = (o2 instanceof PriorityOrdered);
  if (p1 && !p2) {
    return -1;
  }
  else if (p2 && !p1) {
    return 1;
  }

  int i1 = getOrder(o1);
  int i2 = getOrder(o2);
  return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0;  
}

上面代码的逻辑: OrderComparator比较器进行排序的时候,若2个对象中有一个对象实现了PriorityOrdered接口,那么这个对象的优先级更高;若2个对象都是PriorityOrdered或Ordered接口的实现类,那么比较Ordered接口的getOrder方法得到order值,值越低,优先级越高。

SpringMVC中Ordered接口例子

修改dispatcher-servlet.xml文件



    
    
        
            
        
    




  
    
   
  
  




当添加了这段代码,那么SpringMVC默认会注入RequestMappingHandlerAdapter和RequestMappingHandlerMapping这两个类。这时项目就会有两个RequestMappingHandlerMapping,那么这个两个RequestMappingHandlerMapping那个优先级高呢?

initHandlerMappings()源码

//DispatcherServlet类中initHandlerMappings()实现
private void initHandlerMappings(ApplicationContext context) {
        this.handlerMappings = null;

        if (this.detectAllHandlerMappings) {
            // Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
            Map matchingBeans =
                    BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
            if (!matchingBeans.isEmpty()) {
                this.handlerMappings = new ArrayList(matchingBeans.values());
                // We keep HandlerMappings in sorted order.
                OrderComparator.sort(this.handlerMappings);
            }
        }
        else {
            try {
                HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
                this.handlerMappings = Collections.singletonList(hm);
            }
            catch (NoSuchBeanDefinitionException ex) {
                // Ignore, we'll add a default HandlerMapping later.
            }
        }

        // Ensure we have at least one HandlerMapping, by registering
        // a default HandlerMapping if no other mappings are found.
        if (this.handlerMappings == null) {
            this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
            if (logger.isDebugEnabled()) {
                logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default");
            }
        }
    }

//OrderComparator类中实现排序的方法
public static void sort(List list) {
        if (list.size() > 1) {
            Collections.sort(list, INSTANCE);
        }
    }

@Override
    public int compare(Object o1, Object o2) {
        return doCompare(o1, o2, null);
    }

    private int doCompare(Object o1, Object o2, OrderSourceProvider sourceProvider) {
        boolean p1 = (o1 instanceof PriorityOrdered);
        boolean p2 = (o2 instanceof PriorityOrdered);
        if (p1 && !p2) {
            return -1;
        }
        else if (p2 && !p1) {
            return 1;
        }

        // Direct evaluation instead of Integer.compareTo to avoid unnecessary object creation.
        int i1 = getOrder(o1, sourceProvider);
        int i2 = getOrder(o2, sourceProvider);
        return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0;
    }

通过上面的代码走读得出:如果配置了,又配置了自定义的RequestMappingHandlerMapping,并且没有设置自定义RequestMappingHandlerMapping的order值。那么配置的RequestMappingHandlerMapping优先级高,因为内部会设置RequestMappingHandlerMapping的order为0。我们可以将自定义的RequestMappingHandlerMapping的order设置为-1提高它的优先级。

备注:
自定义的RequestMappingHandlerAdapter和的优先级?自定义AdapterRequestMappingHandlerAdapter没有设置order的值情况下,* 谁先定义谁的优先级高*。具体我们可以走读initHandlerAdapters(context)。

作者:忘净空
链接:https://www.jianshu.com/p/d87e5de747a5
來源:
著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

你可能感兴趣的:(Spring之Ordered)