美文网首页
AnnotationAwareOrderComparator :

AnnotationAwareOrderComparator :

作者: rock_fish | 来源:发表于2019-11-19 15:06 被阅读0次

    spring组件的排序相关的接口和注解有这么几个,列举的可能还不全(不断的学习梳理。。)

    两个接口 :org.springframework.core.Orderedorg.springframework.core.PriorityOrdered
    两个注解 org.springframework.core.annotation.Order,javax.annotation.Priority

    通过 AnnotationAwareOrderComparator 实现他们的排序逻辑
    分两个等级,贵族系统的是org.springframework.core.PriorityOrdered,其他的几个是平民血统的。

    • 不同血统优先级不同,贵族的都比平民的高
      1. PriorityOrdered接口 优先级最高
      2. Ordered接口 与 @Order 与 @ Priority 优先级低于 PriorityOrdered接口
    • 相同血统的的通过order的值大小来排序,值越小优先级越高

    以上逻辑来自源码 org.springframework.core.OrderComparator#doCompare

        private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) {
                    //如果有PriorityOrdered 接口的,则 PriorityOrdered 优先级比其他的都高
            boolean p1 = (o1 instanceof PriorityOrdered);
            boolean p2 = (o2 instanceof PriorityOrdered);
            if (p1 && !p2) {
                return -1;
            }
            else if (p2 && !p1) {
                return 1;
            }
            //如果都是PriorityOrdered 通过order值排序,值小优先级高
            //如果都不是PriorityOrdered,即是其他的接口和注解,通过order值来排序,值越小优先级越高
            int i1 = getOrder(o1, sourceProvider);
            int i2 = getOrder(o2, sourceProvider);
            return Integer.compare(i1, i2);
        }
    

    getOrder就是拿order的值自己可以跟下源码。

    他们的区别从文档看

    @Order的注释上看

    @Order defines the sort order for an annotated component.
    The value is optional and represents an order value as defined in the Ordered interface. Lower values have higher priority. The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).
    NOTE: Since Spring 4.0, annotation-based ordering is supported for many kinds of components in Spring, even for collection injection where the order values of the target components are taken into account (either from their target class or from their @Bean method). While such order values may influence priorities at injection points, please be aware that they do not influence singleton startup order which is an orthogonal concern determined by dependency relationships and @DependsOn declarations (influencing a runtime-determined dependency graph).
    Since Spring 4.1, the standard javax.annotation.Priority annotation can be used as a drop-in replacement for this annotation in ordering scenarios. Note that @Priority may have additional semantics when a single element has to be picked (see AnnotationAwareOrderComparator.getPriority).
    Alternatively, order values may also be determined on a per-instance basis through the Ordered interface, allowing for configuration-determined instance values instead of hard-coded values attached to a particular class.
    Consult the javadoc for OrderComparator for details on the sort semantics for non-ordered objects.
    百度翻译

    @Order定义带注释组件的排序顺序。
    该值是可选的,表示order接口中定义的order值。值越低优先级越高。默认值Ordered.LOWEST_PRECEDENCE,表示最低优先级(丢失到任何其他指定的顺序值)。
    注意:自Spring4.0以来,Spring中的许多组件都支持基于注释的排序,即使是考虑了目标组件的顺序值的集合注入(从它们的目标类或@Bean方法)。虽然这些顺序值可能会影响注入点的优先级,但请注意,它们不会影响单例启动顺序,单例启动顺序是由依赖关系和@DependsOn声明确定的正交关系(影响由运行时确定的依赖关系图)。
    自Spring4.1以来,标准的javax.annotation.Priority注释可以在排序场景中用作此注释的替换项。注意,@Priority在必须选择单个元素时可能有额外的语义(请参见annotationawarereordcomparator.getPriority)。
    或者,顺序值也可以通过Ordered接口基于每个实例来确定,允许由配置确定的实例值,而不是附加到特定类的硬编码值。

    有关非有序对象排序语义的详细信息,请参阅OrderComparator的javadoc。
    大致中文的意思是

    1. @Order 注解指定组件的排序顺序。
    2. 从spring 4.0 开始支持注解方式指定组件顺序
    3. 从spring 4.1 开始用javax.annotation.Priority作为标准用法来替代@Order,
    4. 但注解方式的值都是写死的不能配置。通过Ordered接口,可以在实现方法中读取配置信息;如此来实现顺序可配置。

    相关文章

      网友评论

          本文标题:AnnotationAwareOrderComparator :

          本文链接:https://www.haomeiwen.com/subject/ljvwictx.html