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, 随便你实现哪一个接口都可以