Spring OrderUtils

  • Author: HuiFer
  • 源码阅读仓库: SourceHot-Spring
  • org.springframework.core.annotation.OrderUtils主要方法如下
    1. getOrder
    2. getPriority
  • 测试类org.springframework.core.annotation.OrderUtilsTests
  1. @Nullable
  2. public static Integer getOrder(Class<?> type) {
  3. // 缓存中获取
  4. Object cached = orderCache.get(type);
  5. if (cached != null) {
  6. // 返回 int
  7. return (cached instanceof Integer ? (Integer) cached : null);
  8. }
  9. /**
  10. * 注解工具类,寻找{@link Order}注解
  11. */
  12. Order order = AnnotationUtils.findAnnotation(type, Order.class);
  13. Integer result;
  14. if (order != null) {
  15. // 返回
  16. result = order.value();
  17. } else {
  18. result = getPriority(type);
  19. }
  20. // key: 类名,value: intValue
  21. orderCache.put(type, (result != null ? result : NOT_ANNOTATED));
  22. return result;
  23. }
  1. @Nullable
  2. public static Integer getPriority(Class<?> type) {
  3. if (priorityAnnotationType == null) {
  4. return null;
  5. }
  6. // 缓存中获取
  7. Object cached = priorityCache.get(type);
  8. if (cached != null) {
  9. // 不为空返回
  10. return (cached instanceof Integer ? (Integer) cached : null);
  11. }
  12. // 注解工具获取注解
  13. Annotation priority = AnnotationUtils.findAnnotation(type, priorityAnnotationType);
  14. Integer result = null;
  15. if (priority != null) {
  16. // 获取 value
  17. result = (Integer) AnnotationUtils.getValue(priority);
  18. }
  19. // 向缓存插入数据
  20. priorityCache.put(type, (result != null ? result : NOT_ANNOTATED));
  21. return result;
  22. }