在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的ApplicationRunner
與CommandLineRunner
接口的辦法,都只在Springboot啓動完成後才執行。