日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

@Order注解和Ordered接口如何发挥作用

發(fā)布時(shí)間:2023/12/13 综合教程 60 生活家
生活随笔 收集整理的這篇文章主要介紹了 @Order注解和Ordered接口如何发挥作用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。