• 常用注解使用总结系列: @Order 注解


    @Order 注解

    @Order注解主要用来控制配置类的加载顺序
    示例代码:

    package com.runlion.tms.admin.constant;
    
    public class AService {
    
    }
    package com.runlion.tms.admin.constant;
    
    public class BService {
    
    }
    package com.runlion.tms.admin.constant;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.annotation.Order;
    
    @Configuration
    @Order(2)
    public class AConfig {
      @Bean
      public AService AService() {
        System.out.println("AService 加载了");
        return new AService();
      }
    
    }
    package com.runlion.tms.admin.constant;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.annotation.Order;
    
    @Configuration
    @Order(1)
    public class BConfig {
      @Bean
      public BService bService() {
        System.out.println("BService 加载了");
        return new BService();
      }
    }

    测试类:

    package com.runlion.tms.admin.constant;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class OrderMain {
      public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext("com.runlion.tms.admin.constant");
      }
    }

    输出结果:
    BService 加载了
    AService 加载了

    因为BService 的@Order(1),所以先打印出来

  • 相关阅读:
    3.24 每日一题题解
    3.23 每日一题题解
    3.22 每日一题题解
    【POJ1222】EXTENDED LIGHTS OUT
    【BZOJ1013】球形空间产生器sphere
    【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法
    【HDU5862】Counting Intersections
    【HDU1542】Atlantis
    【自定义】抢妹子大作战
    【HDU5361】In Touch
  • 原文地址:https://www.cnblogs.com/lm970585581/p/13066592.html
Copyright © 2020-2023  润新知