Order注解用于排序

  1. public @interface Order {
  2. /**
  3. * The order value.
  4. * <p>Default is {@link Ordered#LOWEST_PRECEDENCE}.
  5. * @see Ordered#getOrder()
  6. */
  7. int value() default Ordered.LOWEST_PRECEDENCE;
  8. }

1. OrderUtils

Spring 提供了 OrderUtils 来获取 Class 的 Order 注解排序信息
扩展: Priority 注解为 javax 扩展注解,功能与 Order 相同

  1. public class OrderUtilsTests {
  2. @Test
  3. public void getSimpleOrder() {
  4. assertEquals(Integer.valueOf(50), OrderUtils.getOrder(SimpleOrder.class, null));
  5. }
  6. @Test
  7. public void getPriorityOrder() {
  8. assertEquals(Integer.valueOf(55), OrderUtils.getOrder(SimplePriority.class, null));
  9. }
  10. @Order(50)
  11. private static class SimpleOrder {}
  12. @Priority(55)
  13. private static class SimplePriority {}
  14. }

2. Ordered接口

对象排序的另一种实现

  1. public interface Ordered {
  2. int getOrder();
  3. }

3. OrderComparator

使用 OrderComparator 来比较2个对象的排序顺序

  1. public final class OrderComparatorTests {
  2. private final OrderComparator comparator = new OrderComparator();
  3. @Test
  4. public void compareOrderedInstancesBefore() {
  5. assertEquals(-1, this.comparator.compare(
  6. new StubOrdered(100), new StubOrdered(2000)));
  7. }
  8. @Test
  9. public void compareOrderedInstancesSame() {
  10. assertEquals(0, this.comparator.compare(
  11. new StubOrdered(100), new StubOrdered(100)));
  12. }
  13. @Test
  14. public void compareOrderedInstancesAfter() {
  15. assertEquals(1, this.comparator.compare(
  16. new StubOrdered(982300), new StubOrdered(100)));
  17. }
  18. private static final class StubOrdered implements Ordered {
  19. private final int order;
  20. public StubOrdered(int order) {
  21. this.order = order;
  22. }
  23. @Override
  24. public int getOrder() {
  25. return this.order;
  26. }
  27. }
  28. }

其内部比较逻辑

  1. return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0;
  1. i1比i2小则返回-1
  2. i1比i2大则返回1
  3. i1等于i2则返回0

    4. AnnotationAwareOrderComparator

    AnnotationAwareOrderComparator继承自OrderComparator
    其可以同时处理对象实现Ordered接口或@Order注解
    其提供了静态方法sort,可以对List进行排序
    1. public class AnnotationAwareOrderComparator extends OrderComparator {
    2. }
    测试代码
    1. public class AnnotationAwareOrderComparatorTests {
    2. @Test
    3. public void sortInstances() {
    4. List<Object> list = new ArrayList<>();
    5. list.add(new B());
    6. list.add(new A());
    7. AnnotationAwareOrderComparator.sort(list);
    8. assertTrue(list.get(0) instanceof A);
    9. assertTrue(list.get(1) instanceof B);
    10. }
    11. @Order(1)
    12. private static class A {
    13. }
    14. @Order(2)
    15. private static class B {
    16. }
    17. }

    5. Bean注册顺序

    Demo2Config的对象将会先于Demo1Config初始化注册
    注意点:其构造函数的初始化并不生效
    1. @Configuration
    2. @Order(2)
    3. public class Demo1Config {
    4. public Demo1Config()
    5. {
    6. System.out.println("Demo1Config");
    7. }
    8. @Bean
    9. public Demo1Service demo1Service(){
    10. System.out.println("demo1config 加载了");
    11. return new Demo1Service();
    12. }
    13. }
    14. @Configuration
    15. @Order(1)
    16. public class Demo2Config {
    17. public Demo2Config()
    18. {
    19. System.out.println("Demo2Config");
    20. }
    21. @Bean
    22. public Demo2Service demo2Service(){
    23. System.out.println("demo2config 加载了");
    24. return new Demo2Service();
    25. }
    26. }
    27. public class Main {
    28. public static void main(String[] args) {
    29. AnnotationConfigApplicationContext context =
    30. new AnnotationConfigApplicationContext("core.annotation.order2");
    31. }
    32. }
    输出的结果信息:
    1. Demo1Config
    2. Demo2Config
    3. demo2config 加载了
    4. demo1config 加载了
    参考
    http://wiselyman.iteye.com/blog/2217192
    https://www.cnblogs.com/syuf/p/6846522.html

作者:兴浩
链接:https://www.jianshu.com/p/8442d21222ef
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。