常用注解使用總結系列: @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),所以先打印出來


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM