素材巴巴 > 程序开发 >

AnnotationAwareOrderComparator.sort 排序规则

程序开发 2023-09-03 09:55:21

我们先看一下AnnotationAwareOrderComparator的关键源码

public class AnnotationAwareOrderComparator extends OrderComparator {protected Integer findOrder(Object obj) {// Check for regular Ordered interfaceInteger order = super.findOrder(obj);if (order != null) {return order;}// Check for @Order and @Priority on various kinds of elementsif (obj instanceof Class) {return OrderUtils.getOrder((Class) obj);}else if (obj instanceof Method) {Order ann = AnnotationUtils.findAnnotation((Method) obj, Order.class);if (ann != null) {return ann.value();}}else if (obj instanceof AnnotatedElement) {Order ann = AnnotationUtils.getAnnotation((AnnotatedElement) obj, Order.class);if (ann != null) {return ann.value();}}else {order = OrderUtils.getOrder(obj.getClass());if (order == null && obj instanceof DecoratingProxy) {order = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());}}return order;}}

可以发现,它是继承了OrderComparator类,那我们也看一下OrderComparator的关键源码

public class OrderComparator implements Comparator {public int compare(@Nullable Object o1, @Nullable Object o2) {return doCompare(o1, o2, null);}private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) {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, sourceProvider);int i2 = getOrder(o2, sourceProvider);return Integer.compare(i1, i2);}private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceProvider) {Integer order = null;if (obj != null && sourceProvider != null) {Object orderSource = sourceProvider.getOrderSource(obj);if (orderSource != null) {if (orderSource.getClass().isArray()) {Object[] sources = ObjectUtils.toObjectArray(orderSource);for (Object source : sources) {order = findOrder(source);if (order != null) {break;}}}else {order = findOrder(orderSource);}}}return (order != null ? order : getOrder(obj));}protected int getOrder(@Nullable Object obj) {if (obj != null) {Integer order = findOrder(obj);if (order != null) {return order;}}return Ordered.LOWEST_PRECEDENCE;}protected Integer findOrder(Object obj) {return (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null);}} 
 

调用AnnotationAwareOrderComparator.sort方法后,程序会进入OrderComparator的doCompare方法。如果其中有一个类是PriorityOrdered的实现类,另一个不是,那么PriorityOrdered实现类的那个排在前面。如果2个都是或2个都不是,那么进入getOrder方法,然后进入AnnotationAwareOrderComparator的findOrder方法,来获取order值。获取order值也有一定的优先级顺序,org.springframework.core.Ordered接口的实现类 > @Order注解 > @Priority注解,如果都没有实现这些接口和注解,就返回Integer.MAX_VALUE。最后根据order值,从小到大排序。

总结:

1、如果1个类实现了PriorityOrdered接口,另一个类没有实现,那么实现的类排前面

2、如果都实现了PriorityOrdered接口,或者都没有实现,那么就要根据order值从小到大排序。order值的获取方式有3种,并且有优先级顺序,Ordered接口(@PriorityOrdered是继承Ordered接口的)> @Order注解 > @Priority,程序只会获取优先级最高的order值。比如既实现了Ordered接口又实现了@Order注解,程序只会取Ordered接口的order值。如果都没有实现,就返回Integer.MAX_VALUE

示例:

创建5个Initializer类,实现ApplicationContextInitializer类接口。这5个类有的实现了PriorityOrdered接口,有的实现了Ordered接口,有的实现了@Order注解,有的实现了@Priority注解

public class MyDemoInitializer1 implements ApplicationContextInitializer, PriorityOrdered {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {}@Overridepublic int getOrder() {return 100;}
 }public class MyDemoInitializer2 implements ApplicationContextInitializer, Ordered {@Overridepublic int getOrder() {return 90;}@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {}}public class MyDemoInitializer3 implements ApplicationContextInitializer, Ordered {@Overridepublic int getOrder() {return 80;}@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {}}@Order(70)
 public class MyDemoInitializer4 implements ApplicationContextInitializer {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {}}@Priority(60)
 public class MyDemoInitializer5 implements ApplicationContextInitializer {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {}}

然后在resources目录下新建META-INFspring.factories文件,在其中配置

org.springframework.context.ApplicationContextInitializer=
 com.caspe.platform.das.initializer.MyDemoInitializer1,
 com.caspe.platform.das.initializer.MyDemoInitializer2,
 com.caspe.platform.das.initializer.MyDemoInitializer3,
 com.caspe.platform.das.initializer.MyDemoInitializer4,
 com.caspe.platform.das.initializer.MyDemoInitializer5

启动程序,AnnotationAwareOrderComparator.sort执行后,我们可以看到结果。MyDemoInitializer1因为实现了PriorityOrdered接口,所有排第一。DelegatingApplicationContextInitializer、SharedMetadataReaderFactoryContextInitializer也实现了Ordered接口,order值是0,所以排2、3位。然后就是4个自定义的MyDemoInitializer,以此类推。


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。