淺談SpringBoot中的Bean初始化方法?@PostConstruct
注解說明
- 使用注解: @PostConstruct
- 效果:在Bean初始化之后(構造方法和@Autowired之后)執(zhí)行指定操作。經(jīng)常用在將構造方法中的動作延遲。
- 備注:Bean初始化時候的執(zhí)行順序: 構造方法 -> @Autowired -> @PostConstruct
代碼示例
注解示例
@Component public class PostConstructTest1 { @Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1() { // postConstructTest2.hello(); } @PostConstruct public void init() { // some init function } }
在Bean的初始化操作中,有時候會遇到調用其他Bean的時候報空指針錯誤。這時候就可以將調用另一個Bean的方法這個操作放到@PostConstruct注解的方法中,將其延遲執(zhí)行。
錯誤示例
@Component public class PostConstructTest1 { @Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1() { postConstructTest2.hello(); } }
@Component public class PostConstructTest2 { public void hello() { System.out.println("hello, i am PostConstructTest2"); } }
正確示例
@Component public class PostConstructTest1 { @Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1() { postConstructTest2.hello(); } }
@Component public class PostConstructTest1 { @Autowired PostConstructTest2 postConstructTest2; public PostConstructTest1() { // postConstructTest2.hello(); } @PostConstruct public void init() { postConstructTest2.hello(); } }
SpringBoot @PostConstruct雖好,也要慎用
做過SpringBoot開發(fā)的話,肯定對@PostConstruct比較熟悉。在一個Bean組件中,標記了@PostConstruct的方法會在Bean構造完成后自動執(zhí)行方法的邏輯。
1 問題的產(chǎn)生
先說下SpringBoot中Bean的加載過程,簡單點說就是SpringBoot會把標記了Bean相關注解(例如@Component、@Service、@Repository等)的類或接口自動初始化全局的單一實例,如果標記了初始化順序會按照用戶標記的順序,否則按照默認順序初始化。在初始化的過程中,執(zhí)行完一個Bean的構造方法后會執(zhí)行該Bean的@PostConstruct方法(如果有),然后初始化下一個Bean。
那么: 如果@PostConstruct方法內的邏輯處理時間較長,就會增加SpringBoot應用初始化Bean的時間,進而增加應用啟動的時間。因為只有在Bean初始化完成后,SpringBoot應用才會打開端口提供服務,所以在此之前,應用不可訪問。
2 案例模擬
為了模擬上面說的情況,在SpringBoot項目中建兩個組件類ComponentOne和ComponentTwo。耗時的初始化邏輯放在ComponentOne中,并設置ComponentOne的初始化順序在ComponentTwo之前。完整代碼如下:
@Component @Order(Ordered.HIGHEST_PRECEDENCE) public class ComponentOne { private Logger logger = LoggerFactory.getLogger(this.getClass()); public ComponentOne() { this.logger.info("ComponentOne 初始化完成"); } @PostConstruct public void init() { this.logger.info("ComponentOne 模擬耗時邏輯開始"); try { //這里休眠5秒模擬耗時邏輯 Thread.sleep(1000 * 5); } catch (InterruptedException e) { logger.info("模擬邏輯耗時失敗", e); } this.logger.info("ComponentOne 模擬耗時邏輯完成"); } }
@Component @Order(Ordered.HIGHEST_PRECEDENCE + 1) public class ComponentTwo { private Logger logger = LoggerFactory.getLogger(this.getClass()); public ComponentTwo() { this.logger.info("ComponentTwo 初始化完成"); } @PostConstruct public void init() { this.logger.info("ComponentTwo 初始化完成后處理"); } }
啟動應用,初始化部分日志如下:
3 總結
所以,如果應用有一些初始化操作,有以下幾點建議:
- 輕量的邏輯可放在Bean的@PostConstruct方法中
- 耗時長的邏輯如果放在@PostConstruct方法中,可使用獨立線程執(zhí)行
- 初始化操作放在CommandLineRunner或ApplicationRunner的實現(xiàn)組件中
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java 數(shù)據(jù)結構鏈表操作實現(xiàn)代碼
這篇文章主要介紹了Java 數(shù)據(jù)結構鏈表操作的相關資料,并附實例代碼,需要的朋友可以參考下2016-10-10StringUtils工具包中字符串非空判斷isNotEmpty和isNotBlank的區(qū)別
今天小編就為大家分享一篇關于StringUtils工具包中字符串非空判斷isNotEmpty和isNotBlank的區(qū)別,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12使用Mybatis遇到的there is no getter異常
這篇文章主要介紹了使用Mybatis遇到的there is no getter異常,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09