Springboot加載所有Bean之后運行方式
Springboot加載所有Bean之后運行方式
Springboot啟動后,需要加載一些配置文件至內(nèi)存中
方法
編寫普通類,繼承ApplicationListener,重寫onApplicationEvent方法
@Component @Slf4j public class InitTaskListener implements ApplicationListener<ContextRefreshedEvent> { @SneakyThrows @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("所有bean加載完之后開始執(zhí)行...."); } }
Springboot中Bean的加載順序
一、為什么要控制
當你在項目啟動時需要提前做一個業(yè)務(wù)的初始化工作時,或者你正在開發(fā)某個中間件需要完成自動裝配時。
你會聲明自己的Configuration類,但是可能你面對的是好幾個有互相依賴的Bean。
如果不加以控制,這時候可能會報找不到依賴的錯誤,這個時候需要通過一些手段來控制springboot中的bean加載順序。
二、怎么控制
@DependsOn
@DependsOn注解可以用來控制bean的創(chuàng)建順序,該注解用于聲明當前bean依賴于另外一個bean。所依賴的bean會被容器確保在當前bean實例化之前被實例化。
與@Component或@Bean配合使用
demo
@Slf4j @Configuration @ConfigurationProperties(prefix = "dict") public class SpringConfig { @Component(value = "EventSource") public class EventSource { public EventSource(){ System.out.println("事件源創(chuàng)建"); } } /** * 監(jiān)聽類 */ @Component @DependsOn(value = {"EventSource"}) public class EventTListener { public EventTListener(){ System.out.println("監(jiān)聽器創(chuàng)建"); } } }
參數(shù)注入
package com.sinosoft.springbootplus.test.config; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; /** * @author lsh * @date 2022/2/25 */ @Slf4j @Configuration @ConfigurationProperties(prefix = "dict") public class SpringConfig { @Component public class Event{ public Event(){ System.out.println("事件事件"); } } @Component public class EventSource{ public EventSource(Event e){ System.out.println("事件源創(chuàng)建"); } } @Component public class EventTListener { public EventTListener(){ System.out.println("監(jiān)聽器創(chuàng)建"); } } }
利用bean的生命周期中的擴展點
@AutoConfigureOrder
@AutoConfigureOrder只能改變外部依賴的@Configuration的順序。
這是不對的用法
@Slf4j @Configuration @ConfigurationProperties(prefix = "dict") public class SpringConfig { @Component @AutoConfigureOrder(1) public class Event{ public Event(){ System.out.println("事件事件"); } } @Component @AutoConfigureOrder(2) public class EventSource{ public EventSource(Event e){ System.out.println("事件源創(chuàng)建"); } } @Component @AutoConfigureOrder(3) public class EventTListener { public EventTListener(){ System.out.println("監(jiān)聽器創(chuàng)建"); } } }
以上內(nèi)容發(fā)現(xiàn),在config里配置是不起作用的。
這是正確的用法
創(chuàng)建兩個配置類
@Slf4j @Configuration @AutoConfigureOrder(1) public class SpringConfig { @Component public class Event{ public Event(){ System.out.println("首先在SpringConfig"); } } } @Slf4j @Configuration @AutoConfigureOrder(2) public class NewConfig { @Component public class Event{ public Event(){ System.out.println("然后在NewConfig"); } } }
測試
發(fā)現(xiàn)結(jié)果是不正確的,注解還是沒有生效。
當前工程里增加配置 META-INF/spring.factories,內(nèi)容為項目中的配置類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sinosoft.springbootplus.common.config.NewConfig,com.sinosoft.springbootplus.common.config.SpringConfig
測試結(jié)果如圖(正確)
三、遇到的問題
1、需要根據(jù)配置決定生成哪個實現(xiàn)類。
當在配置文件中配置的dict.cacheType的值是local時,初始化LocalISysDictRepository交給spring容器管理;當項目依賴了redis并且配置文件中配置的dict.cacheType的值是redis時,初始化RedisISysDictRepository交給spring容器管理。
2、但是我又在這兩個實現(xiàn)類上加了@Repository注解,也要交給Spring管理,這個時候項目啟動就報錯了。(通俗的來說一個類只能一次交給Spring管理)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis resultmap 如何為對象賦值的調(diào)用順序
這篇文章主要介紹了mybatis resultmap 如何為對象賦值的調(diào)用順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01springboot實現(xiàn)上傳并解析Excel過程解析
這篇文章主要介紹了springboot實現(xiàn)上傳并解析Excel過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09