我正在使用 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
方法中。
添加回答
舉報
0/150
提交
取消