1. 實現ApplicationRunner接口, 重寫run方法
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Slf4j
@Component
@Order(2) //order 值越小越先執行
public class MyAppRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) {createHttpClient();}private void createHttpClient() {log.info("項目啟動了, 執行--------ApplicationRunner");// TODO}
}
?
2. 實現CommandLineRunner接口, 重寫run方法
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Slf4j
@Component
@Order(3) //order 值越小越先執行
public class MyComRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {createHttpClient();}private void createHttpClient() {log.info("項目啟動了, 執行--------CommandLineRunner");// TODO}
}
?
執行結果
ApplicationRunner和CommandLineRunner, 隨便你實現哪一個接口都可以
?