AnnotationAwareOrderComparator类分析

发布 : 2019-04-25 分类 : spring 浏览 :

解析

类继承了OrderComparator

1
2
public class AnnotationAwareOrderComparator extends OrderComparator {
}

  • 属性就一个,该对象的实例

    1
    2
    //public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
    public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
  • 剩下的都是静态方法
    这个方法的每一行都比较清晰。通过这个方法能学到不少东西:
    1、对于类,我们能通过OrderUtils.getOrder((Class<?>) obj)获取
    2、对于注解类型,我们能通过AnnotationUtils.getAnnotation来获取注解内容。
    3、方法,我们通过AnnotationUtils.findAnnotation((Method) obj, Order.class);获取。大家可以自行实验下。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    @Override
    @Nullable
    protected Integer findOrder(Object obj) {
    // Check for regular Ordered interface
    Integer order = super.findOrder(obj);
    if (order != null) {
    return order;
    }

    // Check for @Order and @Priority on various kinds of elements
    if (obj instanceof Class) {
    return OrderUtils.getOrder((Class<?>) obj);
    }
    else if (obj instanceof Method) {
    Order ann = AnnotationUtils.findAnnotation((Method) obj, Order.class);
    if (ann != null) {
    return ann.value();
    }
    }
    else if (obj instanceof AnnotatedElement) {
    Order ann = AnnotationUtils.getAnnotation((AnnotatedElement) obj, Order.class);
    if (ann != null) {
    return ann.value();
    }
    }
    else {
    order = OrderUtils.getOrder(obj.getClass());
    if (order == null && obj instanceof DecoratingProxy) {
    order = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());
    }
    }

    return order;
    }
本文作者 : braveheart
原文链接 : https://zhangjun075.github.io/passages/AnnotationAwareOrderComparator类分析/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我打赏

微信扫一扫, 向我打赏

支付宝扫一扫, 向我打赏

支付宝扫一扫, 向我打赏

留下足迹