zoukankan      html  css  js  c++  java
  • @Order注解和Ordered接口如何发挥作用

    1、@Order注解与Ordered接口实现相同的逻辑

    @Order实现的是同一类组件或者bean的执行顺序,不是实例化顺序,也不是组件在IOC容器的注入顺序。

    逻辑解析:

    • 存在多个同类(接口)组件,组件之间可能需要按某个顺序执行,使用@Order注解标注执行顺序;
    • 组件会在各业务配置阶段被无序的加入到容器中,被容器包裹,比如ArrayList中添加需要执行的组件,暂时称这个容器为listA;
    • 此时,业务类B持有容器ListA,它需要按顺序注册或者执行容器里的组件;
    • 常规情况下,业务类B会调用AnnotationAwareOrderComparator.sort(listA)对容器里的组件按@Order注解排序,如果没有Order注解则排最后;
    • 最后,按照业务逻辑循环listA,取出每一个组件执行业务逻辑。

    2、AnnotationAwareOrderComparator

    AnnotationAwareOrderComparator是OrderComparator的子类,它支持Spring的org.springframework.core.Ordered接口以及@Order和@Priority注解,其中Ordered接口实例提供的order值将覆盖静态定义的注解值(如果有)。

    AnnotationAwareOrderComparator就是比较器的Order注解和接口实现。

    AnnotationAwareOrderComparator -> OrderComparator -> Comparator
    

    以CommandLineRunner接口为例,我们可以在实现CommandLineRunner接口的类上标注@Order注解以便为多个CommandLineRunner执行排序。

    在Springboot启动时,对CommandLineRunner接口进行排序并执行

    	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);
    			}
    		}
    	}
    

    可以看到调用接口前通过AnnotationAwareOrderComparator.sort(runners)静态方法对所有ApplicationRunner和CommandLineRunner进行排序。

    在调用链从AnnotationAwareOrderComparator的findOrderFromAnnotation到OrderUtils的findOrder静态方法获取到类上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、结论

    在Springboot中,组件容器的持有者需要在逻辑执行前调用AnnotationAwareOrderComparator.sort,给容器内的组件排序,然后标注过@Order注解、@Priority注解或者实现Ordered接口的组件才能才能按既定的排序执行。

  • 相关阅读:
    性能测试
    怎样开始用selenium进行自动化测试
    手机自动化测试的原理
    黑盒测试与白盒测试的区别
    白盒测试方法
    黑盒测试概念及设计方法
    接口测试的概念及常用方法
    运用c语言和Java写九九乘法表
    appium键值对的应用
    压力测试和负载测试的区别
  • 原文地址:https://www.cnblogs.com/asker009/p/14049194.html
Copyright © 2011-2022 走看看