Spring核心接口之Ordered
一、Ordered接口介绍
Spring中提供了一个Ordered接口。从单词意思就知道Ordered接口的作用就是用来排序的。
Spring框架是一个大量使用策略设计模式的框架,这意味着有很多相同接口的实现类,那么必定会有优先级的问题。于是Spring就提供了Ordered这个接口,来处理相同接口实现类的优先级问题。
二、Ordered接口分析
1、Ordered接口的定义:
public interface Ordered {/*** Useful constant for the highest precedence value. * @see java.lang.Integer#MIN_VALUE */int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;/** * Useful constant for the lowest precedence value. * @see java.lang.Integer#MAX_VALUE */int LOWEST_PRECEDENCE = Integer.MAX_VALUE;/** * Get the order value of this object. * Higher values are interpreted as lower priority. As a consequence, * the object with the lowest value has the highest priority (somewhat * analogous to Servlet {@code load-on-startup} values). *
Same order values will result in arbitrary sort positions for the * affected objects. * @return the order value * @see #HIGHEST_PRECEDENCE * @see #LOWEST_PRECEDENCE */int getOrder();
}
该接口卡只有1个方法getOrder()及 2个变量HIGHEST_PRECEDENCE最高级(数值最小)和LOWEST_PRECEDENCE最低级(数值最大)。
2、OrderComparator类:实现了Comparator接口的一个比较器。
public class OrderComparator implements Comparator
提供了2个静态排序方法:sort(List> list)用来排序list集合、sort(Object[] array)用来排序Object数组
可以下OrderComparator类的public int compare(Object o1, Object o2)方法,可以看到另外一个类PriorityOrdered,这个方法的逻辑解析如下:
- 若对象o1是Ordered接口类型,o2是PriorityOrdered接口类型,那么o2的优先级高于o1
- 若对象o1是PriorityOrdered接口类型,o2是Ordered接口类型,那么o1的优先级高于o2
- 其他情况,若两者都是Ordered接口类型或两者都是PriorityOrdered接口类型,调用Ordered接口的getOrder方法得到order值,order值越大,优先级越小
简单来说就是:
OrderComparator比较器进行排序的时候,若2个对象中有一个对象实现了PriorityOrdered接口,那么这个对象的优先级更高。若2个对象都是PriorityOrdered或Ordered接口的实现类,那么比较Ordered接口的getOrder方法得到order值,值越低,优先级越高。
三、Spring中使用Ordered接口在的例子
在spring配置文件中添加:
当我们配置了annotation-driven以及这两个bean的时候。Spring容器就有了2个RequestMappingHandlerAdapter和2个RequestMappingHandlerMapping。
DispatcherServlet内部有HandlerMapping(RequestMappingHandlerMapping是其实现类)集合和HandlerAdapter(RequestMappingHandlerAdapter是其实现类)集合。
//RequestMappingHandlerMapping集合
private List
//HandlerAdapter集合
private List
在仔细看下DispatcherServlet类的private void initHandlerMappings(ApplicationContext context)方法可以看到如下代码:
//detectAllHandlerMappings默认为true
if (this.detectAllHandlerMappings) {
// Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
Map
BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList
// We keep HandlerMappings in sorted order.
//进行排序
AnnotationAwareOrderComparator.sort(this.handlerMappings);
}
}
AnnotationAwareOrderComparator继承了OrderComparator类
再看下
br/>@Bean
br/>@Bean
进入RequestMappingHandlerMapping和RequestMappingHandlerAdapter代码里面看看它们的order属性是如何定义的。RequestMappingHandlerMapping// Ordered.LOWEST_PRECEDENCE只为Integer.MAX_VALUEpublic abstract class AbstractHandlerMapping extends WebApplicationObjectSupport implements HandlerMapping, Ordered { private int order = Integer.MAX_VALUE; AbstractHandlerMapping是RequestMappingHandlerMapping的父类。RequestMappingHandlerAdapterpublic abstract class AbstractHandlerMethodAdapter extends WebContentGenerator implements HandlerAdapter, Ordered { // Ordered.LOWEST_PRECEDENCE只为Integer.MAX_VALUE private int order = Ordered.LOWEST_PRECEDENCE;AbstractHandlerMethodAdapter是RequestMappingHandlerAdapter的父类。 可以看到RequestMappingHandlerMapping和RequestMappingHandlerAdapter没有设置order属性的时候,order属性的默认值都是Integer.MAX_VALUE,即优先级最低。
总结: 如果配置了
如果配置了
四、应用
1、定义接口
import java.util.Map;
import org.springframework.core.Ordered;
public interface Filter extends Ordered{ public void doFiler(Map prams);}
2、实现接口
import java.util.Map; @Component public class LogFilter implements Filter { private int order =1; public int getOrder() { return order; } public void setOrder(int order) { this.order = order; } public void doFiler(Map prams) { System.out.println("打印日志"); } }import java.util.Map;@Componentpublic class PowerLogFilter implements Filter { private int order =2; public int getOrder() { return order; } public void setOrder(int order) { this.order = order; } public void doFiler(Map prams) { System.out.println("权限控制"); }}3、测试进行排序public static void main(String[] args) throws Exception {String config = Test.class.getPackage().getName().replace('.', '/') + "/bean.xml"; ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config); context.start(); Map filters = context.getBeansOfType(Filter.class); System.out.println(filters.size()); List f= new ArrayList(filters.values()); OrderComparator.sort(f); for(int i=0; i params = new HashMap(); f.get(i).doFiler(params); }}
4、配置文件