若是你想在Spring Boot啓動的時候運行一些特定的代碼,你能夠實現接口ApplicationRunner
或者CommandLineRunner
,這兩個接口實現方式同樣,它們都只提供了一個run方法。spring
CommandLineRunner:啓動獲取命令行參數。微信
public interface CommandLineRunner { /** * Callback used to run the bean. * @param args incoming main method arguments * @throws Exception on error */ void run(String... args) throws Exception; }
ApplicationRunner:啓動獲取應用啓動的時候參數。app
public interface ApplicationRunner { /** * Callback used to run the bean. * @param args incoming application arguments * @throws Exception on error */ void run(ApplicationArguments args) throws Exception; }
import org.springframework.boot.* import org.springframework.stereotype.* @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } }
或者這樣命令行
@Bean public CommandLineRunner init() { return (String... strings) -> { }; }
推薦:Spring Boot & Cloud 最強技術教程code
掃描關注咱們的微信公衆號,乾貨天天更新。教程