推荐实现CommandLineRunner
使用方法:
自定义Class 通过@Component标识,如果有多个CommandLineRunner 的实现类,可以通过@Order 去区分优先级
比较 @PostConstruct VS 实现CommandLineRunner
1. @PostConstruct 在服务器加载Servlet的时候运行,PreDestroy()之前执行
2. 实现CommandLineRunner 在Spring Boot服务启动完成之后执行
/**
* 当项目启动的之后调用当前方法
* @actuor youshang
* @create 2021-09-14 10:06
* @desc
*/
@Component
public class InitJob implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("执行方法: InitJob run");
}
}