Spring Boot 启动时执行加载资源/执行操作

来源:互联网 发布:怎么向淘宝网投诉 编辑:程序博客网 时间:2024/05/18 09:42

SpringBoot 在启动的时候加载资源或者执行操作,进行初始化来执行特定操作,SpringBoot已经提供了这样的接口,通过实现该接口就可以实现需要的操作


实现CommandLineRunner接口

@Order(value=2)@Componentpublic class CommandLineRunnerListenerImpl implements CommandLineRunner {    @Override    public void run(String... args) throws Exception {       // 执行操作    }}
  • 可以通过指定@Order的值来控制启动的顺序,值越小表示越先执行

实现ApplicationListener接口

@Componentpublic class CommandLineRunnerListenerImpl implements ApplicationListener {    @Override    public void onApplicationEvent(ApplicationEvent event) {        // 执行操作    }}
原创粉丝点击