@Order注解和Ordered接口如何发挥作用
1、@Order注解與Ordered接口實(shí)現(xiàn)相同的邏輯
@Order實(shí)現(xiàn)的是同一類(lèi)組件或者bean的執(zhí)行順序,不是實(shí)例化順序,也不是組件在IOC容器的注入順序。
邏輯解析:
存在多個(gè)同類(lèi)(接口)組件,組件之間可能需要按某個(gè)順序執(zhí)行,使用@Order注解標(biāo)注執(zhí)行順序;
組件會(huì)在各業(yè)務(wù)配置階段被無(wú)序的加入到容器中,被容器包裹,比如ArrayList中添加需要執(zhí)行的組件,暫時(shí)稱(chēng)這個(gè)容器為listA;
此時(shí),業(yè)務(wù)類(lèi)B持有容器ListA,它需要按順序注冊(cè)或者執(zhí)行容器里的組件;
常規(guī)情況下,業(yè)務(wù)類(lèi)B會(huì)調(diào)用AnnotationAwareOrderComparator.sort(listA)對(duì)容器里的組件按@Order注解排序,如果沒(méi)有Order注解則排最后;
最后,按照業(yè)務(wù)邏輯循環(huán)listA,取出每一個(gè)組件執(zhí)行業(yè)務(wù)邏輯。
2、AnnotationAwareOrderComparator
AnnotationAwareOrderComparator是OrderComparator的子類(lèi),它支持Spring的org.springframework.core.Ordered接口以及@Order和@Priority注解,其中Ordered接口實(shí)例提供的order值將覆蓋靜態(tài)定義的注解值(如果有)。
AnnotationAwareOrderComparator就是比較器的Order注解和接口實(shí)現(xiàn)。
AnnotationAwareOrderComparator -> OrderComparator -> Comparator
以CommandLineRunner接口為例,我們可以在實(shí)現(xiàn)CommandLineRunner接口的類(lèi)上標(biāo)注@Order注解以便為多個(gè)CommandLineRunner執(zhí)行排序。
在Springboot啟動(dòng)時(shí),對(duì)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)用接口前通過(guò)AnnotationAwareOrderComparator.sort(runners)靜態(tài)方法對(duì)所有ApplicationRunner和CommandLineRunner進(jìn)行排序。
在調(diào)用鏈從AnnotationAwareOrderComparator的findOrderFromAnnotation到OrderUtils的findOrder靜態(tài)方法獲取到類(lè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)注過(guò)@Order注解、@Priority注解或者實(shí)現(xiàn)Ordered接口的組件才能才能按既定的排序執(zhí)行。
總結(jié)
以上是生活随笔為你收集整理的@Order注解和Ordered接口如何发挥作用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 图像质量评价指标之 PSNR 和 SSI
- 下一篇: 常用数学符号读法及其含义