ApplicationRunner

实现ApplicationRunner接口,并重写run函数,可以实现springboot项目启动完成之后的回调

@Configuration
public class MyApplicationRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
       //something to do 
    }
}

CommandLineRunner

实现CommandLineRunner接口,并重写run函数,可以实现springboot项目启动完成之后的回调

@Configuration
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {

    }
}

@PreDestroy

采用@PreDestroy注释可以实现springboot项目停止之后的钩子回调,运用实例如下所示:

@Configuration
public class MyPreDestroy {

    @PreDestroy
    public void myPreDestroy() {
        
    
    }

}