AnnotationAwareOrderComparator.sort 排序规则

我们先看一下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<Object> {

	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<ConfigurableApplicationContext>, PriorityOrdered {

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
	}

	@Override
	public int getOrder() {
		return 100;
	}
}

public class MyDemoInitializer2 implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
	
	@Override
	public int getOrder() {
		return 90;
	}

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		
	}

}

public class MyDemoInitializer3 implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {

	@Override
	public int getOrder() {
		return 80;
	}

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		
	}

}

@Order(70)
public class MyDemoInitializer4 implements ApplicationContextInitializer<ConfigurableApplicationContext> {

	
	
	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		
	}

}

@Priority(60)
public class MyDemoInitializer5 implements ApplicationContextInitializer<ConfigurableApplicationContext> {

	@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,以此类推。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值