首页 > 其他 > 详细

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

时间:2020-06-08 18:32:48      阅读:88      评论:0      收藏:0      [点我收藏+]

@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),所以先打印出来

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

原文:https://www.cnblogs.com/lm970585581/p/13066592.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!