我们先看一下AnnotationAwareOrderComparator的关键源码
public class AnnotationAwareOrderComparator extends OrderComparator {
protected Integer findOrder(Object obj) {
// Check for regular Ordered interface
Integer order = super.findOrder(obj);
if (order != null) {
return order;
}
// Check for @Order and @Priority on various kinds of elements
if (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
调用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 {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
}
@Override
public int getOrder() {
return 100;
}
}
public class MyDemoInitializer2 implements ApplicationContextInitializer, Ordered {
@Override
public int getOrder() {
return 90;
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
}
}
public class MyDemoInitializer3 implements ApplicationContextInitializer, Ordered {
@Override
public int getOrder() {
return 80;
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
}
}
@Order(70)
public class MyDemoInitializer4 implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
}
}
@Priority(60)
public class MyDemoInitializer5 implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
}
}
然后在resources目录下新建META-INF\spring.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,以此类推。