@Order注解和Ordered接口如何发挥作用
1、@Order注解與Ordered接口實現(xiàn)相同的邏輯
@Order實現(xiàn)的是同一類組件或者bean的執(zhí)行順序,不是實例化順序,也不是組件在IOC容器的注入順序。
邏輯解析:
存在多個同類(接口)組件,組件之間可能需要按某個順序執(zhí)行,使用@Order注解標(biāo)注執(zhí)行順序;
組件會在各業(yè)務(wù)配置階段被無序的加入到容器中,被容器包裹,比如ArrayList中添加需要執(zhí)行的組件,暫時稱這個容器為listA;
此時,業(yè)務(wù)類B持有容器ListA,它需要按順序注冊或者執(zhí)行容器里的組件;
常規(guī)情況下,業(yè)務(wù)類B會調(diào)用AnnotationAwareOrderComparator.sort(listA)對容器里的組件按@Order注解排序,如果沒有Order注解則排最后;
最后,按照業(yè)務(wù)邏輯循環(huán)listA,取出每一個組件執(zhí)行業(yè)務(wù)邏輯。
2、AnnotationAwareOrderComparator
AnnotationAwareOrderComparator是OrderComparator的子類,它支持Spring的org.springframework.core.Ordered接口以及@Order和@Priority注解,其中Ordered接口實例提供的order值將覆蓋靜態(tài)定義的注解值(如果有)。
AnnotationAwareOrderComparator就是比較器的Order注解和接口實現(xiàn)。
AnnotationAwareOrderComparator -> OrderComparator -> Comparator
以CommandLineRunner接口為例,我們可以在實現(xiàn)CommandLineRunner接口的類上標(biāo)注@Order注解以便為多個CommandLineRunner執(zhí)行排序。
在Springboot啟動時,對CommandLineRunner接口進(jìn)行排序并執(zhí)行
private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList<>();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
for (Object runner : new LinkedHashSet<>(runners)) {
if (runner instanceof ApplicationRunner) {
callRunner((ApplicationRunner) runner, args);
}
if (runner instanceof CommandLineRunner) {
callRunner((CommandLineRunner) runner, args);
}
}
}
可以看到調(diào)用接口前通過AnnotationAwareOrderComparator.sort(runners)靜態(tài)方法對所有ApplicationRunner和CommandLineRunner進(jìn)行排序。
在調(diào)用鏈從AnnotationAwareOrderComparator的findOrderFromAnnotation到OrderUtils的findOrder靜態(tài)方法獲取到類上Order注解。
AnnotationAwareOrderComparator
@Nullable
private Integer findOrderFromAnnotation(Object obj) {
AnnotatedElement element = (obj instanceof AnnotatedElement ? (AnnotatedElement) obj : obj.getClass());
MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY);
Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
if (order == null && obj instanceof DecoratingProxy) {
return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
}
return order;
}
OrderUtils
@Nullable
private static Integer findOrder(MergedAnnotations annotations) {
MergedAnnotation<Order> orderAnnotation = annotations.get(Order.class);
if (orderAnnotation.isPresent()) {
return orderAnnotation.getInt(MergedAnnotation.VALUE);
}
MergedAnnotation<?> priorityAnnotation = annotations.get(JAVAX_PRIORITY_ANNOTATION);
if (priorityAnnotation.isPresent()) {
return priorityAnnotation.getInt(MergedAnnotation.VALUE);
}
return null;
}
3、結(jié)論
在Springboot中,組件容器的持有者需要在邏輯執(zhí)行前調(diào)用AnnotationAwareOrderComparator.sort,給容器內(nèi)的組件排序,然后標(biāo)注過@Order注解、@Priority注解或者實現(xiàn)Ordered接口的組件才能才能按既定的排序執(zhí)行。
總結(jié)
以上是生活随笔為你收集整理的@Order注解和Ordered接口如何发挥作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像质量评价指标之 PSNR 和 SSI
- 下一篇: 常用数学符号读法及其含义