使用@Order調整配置類加載順序


轉自:https://blog.csdn.net/qq_15037231/article/details/78158553

4.1 @Order

  • Spring 4.2 利用@Order控制配置類的加載順序

4.2 演示

  • 兩個演示bean
package com.wisely.spring4_2.order;

public class Demo1Service {

}

package com.wisely.spring4_2.order;

public class Demo2Service {

}

  • 兩個配置類,注意@Order配置加載的順序
package com.wisely.spring4_2.order; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; @Configuration @Order(2) public class Demo1Config { @Bean public Demo1Service demo1Service(){ System.out.println("demo1config 加載了"); return new Demo1Service(); } } 
package com.wisely.spring4_2.order; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; @Configuration @Order(1) public class Demo2Config { @Bean public Demo2Service demo2Service(){ System.out.println("demo2config 加載了"); return new Demo2Service(); } } 
  • 運行
package com.wisely.spring4_2.order; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wisely.spring4_2.order"); } } 

輸出結果

demo2config 加載了
demo1config 加載了

讀者可自己調整順序在運行


免責聲明!

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



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