常用注解使用总结系列: @Order 注解
发布日期:2021-05-09 04:56:28 浏览次数:15 分类:博客文章

本文共 1253 字,大约阅读时间需要 4 分钟。

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

上一篇:@EnableDiscoveryClient 注解如何实现服务注册与发现
下一篇:@InitBinder的作用

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年03月31日 13时43分18秒