亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring boot bean初始化順序

Spring boot bean初始化順序

瀟瀟雨雨 2023-06-14 16:45:49
我正在使用 spring boot 開發一個簡單的 REST API,但我堅持使用 spring 的命令來初始化我的應用程序的 bean。我如何控制我的應用程序中 bean 初始化的順序?我正在使用 spring boot 2.1.7 問題是 spring 試圖在 ContactRestController 依賴的 SomeService 之前初始化 ContactRestController 所以它在 ContactRestController 的構造函數中以 NullPointerException 結束:@RestControllerpublic class ContactRestController {    @Autowired    private SomeService ;   // no-args constructor     public ContactRestControlle(){       this.someService.doStuff() ;    }}
查看完整描述

1 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

作為參數添加SomeService到構造函數,并@Autowired從字段中刪除。現在它不可能為空。

或者,將構造函數中的代碼移動到@PostConstruct方法中。


查看完整回答
反對 回復 2023-06-14
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號