SpringBoot项目启动后执行任务的3种方法
@PostConstruct注解
在spring进行项目启动时,构造器@Construvtor,@Autowired,@PostConstruct,三者的执行顺序?
@Constructor > @Autowired > @PostConstruct(具体原因可以 查看文档)
使用方法:将要被启动执行的类上加上@Component注解,然后在需要执行的方法上@PostConstruct注解
实现ApplicationRunner接口
@Component注解相应的类,并重写run方法,若存在多个启动执行的任务,可利用在类上使用@Order注解来指定顺序
实现CommandLineRunner接口
mmandLineRunner接口
基本和实现ApplicationRunner接口的操作相同,唯一区别就是实现的接口不同