Springboot中的@Order如何使用
在spring-boot 2.6.2下測(cè)試,@Order并不會(huì)影響bean的裝載順序,聲明了@Component的類,無(wú)論是構(gòu)造方法、@PostConstruct注解聲明的方法,還是實(shí)現(xiàn)的InitializingBean接口中的afterPropertiesSet()方法,如果beanClass位于同樣的目錄層級(jí),這些方法的調(diào)用只會(huì)受到className的順序影響:
@Component
@Slf4j
@Order(2)
public class Bean1 implements InitializingBean {
? ? public Bean1() {
? ? ? ? log.info("construct bean1");
? ? }
? ? @Override
? ? public void afterPropertiesSet() throws Exception {
? ? ? ? log.info("initialled bean1");
? ? }
? ? @PostConstruct
? ? public void post() {
? ? ? ? log.info("post bean1");
? ? }
}
@Component
@Slf4j
@Order(1)
public class Bean2 implements InitializingBean {
? ? public Bean2() {
? ? ? ? log.info("construct bean2");
? ? }
? ? @Override
? ? public void afterPropertiesSet() throws Exception {
? ? ? ? log.info("initialled bean2");
? ? }
? ? @PostConstruct
? ? public void post() {
? ? ? ? log.info("post bean2");
? ? }
}
/* 結(jié)果打印順序:
construct bean1
post bean1
initialled bean1
construct bean2
post bean2
initialled bean2
*/觀察@Order的注解說(shuō)明,第一句寫(xiě)著: @Order defines the sort order for an annotated component. 提到這個(gè)注解只是對(duì)component排序,那么哪里會(huì)收到這個(gè)排序數(shù)值的影響呢?
這里先改造一下代碼:
public interface IBean {
? ? void work();
}
@Component
@Slf4j
@Order(2)
public class Bean1 implements InitializingBean,CommandLineRunner,IBean {
? ? public Bean1() {
? ? ? ? log.info("construct bean1");
? ? }
? ? @Override
? ? public void afterPropertiesSet() throws Exception {
? ? ? ? log.info("initialled bean1");
? ? }
? ? @PostConstruct
? ? public void post() {
? ? ? ? log.info("post bean1");
? ? }
? ? @Override
? ? public void run(String... args) throws Exception {
? ? ? ? log.info("running bean1");
? ? }
? ? @Override
? ? public void work() {
? ? ? ? log.info("bean1 is working");
? ? }
}
@Component
@Slf4j
@Order(1)
public class Bean2 implements InitializingBean, CommandLineRunner, IBean {
? ? public Bean2() {
? ? ? ? log.info("construct bean2");
? ? }
? ? @Override
? ? public void afterPropertiesSet() throws Exception {
? ? ? ? log.info("initialled bean2");
? ? }
? ? @PostConstruct
? ? public void post() {
? ? ? ? log.info("post bean2");
? ? }
? ? @Override
? ? public void run(String... args) throws Exception {
? ? ? ? log.info("running bean2");
? ? }
? ? @Override
? ? public void work() {
? ? ? ? log.info("bean2 is working");
? ? }
}
@Service
@RequiredArgsConstructor
public class TestService {
? ? private final List<IBean> beans;
? ? public void test(){
? ? ? ? beans.forEach(IBean::work);
? ? }
}啟動(dòng)之后執(zhí)行TestService的test方法,得到如下順序的日志:
construct bean1
post bean1
initialled bean1
construct bean2
post bean2
initialled bean2
running bean2
running bean1
bean2 is working
bean1 is working
作一下說(shuō)明,@Order會(huì)影響依賴注入的順序,如果存在同樣類型的多個(gè)bean,且依賴聲明使用了List<BeanInterface>,會(huì)將所有bean實(shí)例按照Order聲明的順序放入一個(gè)ArrayList中注入,如果用的是Collection或者Set則無(wú)效,因?yàn)轭愋捅旧頍o(wú)序。
而CommandLineRunner聲明的run方法,會(huì)在bean被IOC容器裝配完成之后被調(diào)用,方法注釋簡(jiǎn)單明了的一句Callback used to run the bean可以理解為bean實(shí)例真正構(gòu)建完成之后的回調(diào)方法,而這個(gè)方法會(huì)受到@Order的順序影響,效果前面日志中已經(jīng)體現(xiàn),這里貼一下類注釋:
Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or @Order annotation.
除了以上兩種用法,@Aspect聲明的切面類、繼承了OncePerRequestFilter的過(guò)濾器等,它們的作用順序也會(huì)受到Order的影響。
注意:如果@Order注解配置在了@Configuration修飾的配置類中的@Bean方法修飾的方法上時(shí),指定順序并不會(huì)生效
順便提一下另外一個(gè)注解:@Priority,以上@Order能起作用的地方,換成@Priority一樣會(huì)生效,但在一種情況下,它的作用和@Order大為不同:
同一個(gè)接口類型有多個(gè)不同的bean實(shí)現(xiàn)類時(shí),注入依賴時(shí)使用集合聲明不會(huì)報(bào)錯(cuò),但聲明為單體類型時(shí),如果各個(gè)Bean類使用了@Order聲明,就會(huì)報(bào)required a single bean, but x were found的錯(cuò)誤,這時(shí)有兩種方法可以解決問(wèn)題,一是在其中一個(gè)Bean類加上@Primary的注解聲明為首要類型,另外一個(gè)就是把Order改成Priority,優(yōu)先級(jí)最高的那個(gè)bean會(huì)被當(dāng)作primary來(lái)對(duì)待。
到此這篇關(guān)于Springboot中的@Order如何使用的文章就介紹到這了,更多相關(guān)Springboot @Order內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 3.4.0 結(jié)合 Mybatis-plus 實(shí)
本文詳細(xì)介紹了在 Spring Boot 3.4.0 項(xiàng)目中結(jié)合 Mybatis-plus 實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的完整方案,通過(guò)自定義注解和AOP切面,我們可以優(yōu)雅地實(shí)現(xiàn)方法級(jí)別的數(shù)據(jù)源切換,滿足多數(shù)據(jù)源場(chǎng)景下的各種需求,感興趣的朋友一起看看吧2025-04-04
springboot整合mybatis的超詳細(xì)過(guò)程(配置模式+注解模式)
這篇文章主要介紹了springboot整合mybatis的詳細(xì)過(guò)程(配置模式+注解模式),這里我使用的是配置模式+注解模式所以需要配置全局文件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
SpringBoot2零基礎(chǔ)到精通之映射與常用注解請(qǐng)求處理
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說(shuō)是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開(kāi)發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)映射、常用注解和方法參數(shù)的小技巧2022-03-03
詳解Spring?Boot中@PostConstruct的使用示例代碼
在Java中,@PostConstruct是一個(gè)注解,通常用于標(biāo)記一個(gè)方法,它表示該方法在類實(shí)例化之后(通過(guò)構(gòu)造函數(shù)創(chuàng)建對(duì)象之后)立即執(zhí)行,這篇文章主要介紹了詳解Spring?Boot中@PostConstruct的使用,需要的朋友可以參考下2023-09-09
Java使用Maven BOM統(tǒng)一管理版本號(hào)的實(shí)現(xiàn)
這篇文章主要介紹了Java使用Maven BOM統(tǒng)一管理版本號(hào)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Hibernate傳入Java對(duì)象創(chuàng)建動(dòng)態(tài)表并錄入數(shù)據(jù)
這篇文章主要介紹了Hibernate傳入Java對(duì)象創(chuàng)建動(dòng)態(tài)表并錄入數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
解讀SpringBoot接收List<Bean>參數(shù)問(wèn)題(POST請(qǐng)求方式)
這篇文章主要介紹了解讀SpringBoot接收List<Bean>參數(shù)問(wèn)題(POST請(qǐng)求方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

