Springboot 在啓動前執行代碼

在Springboot自動實例化類前,我們需要準備一些數據在類型實例化的時候使用。 經過測試,在啓動類添加註解@PostConstruct有效。

@SpringBootApplication
public class MyApplication {
	public static void main(String[] args) {
		SpringApplication.run(MyApplication.class, args);
	}
	// springboot正式啓動前
	@PostConstruct
	public void postConstruct(){
		System.out.println("執行Springboot正式啓動前的代碼")
	}
}

實現Spring的ApplicationRunnerCommandLineRunner接口的辦法,都只在Springboot啓動完成後才執行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章