java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot启动后执行方法

详解SpringBoot启动项目后执行方法的几种方式

作者:无梦.

在项目开发中某些场景必须要用到启动项目后立即执行方式的功能,本文主要聊聊实现立即执行的几种方法,具有一定的参考价值,感兴趣的可以了解一下

在项目开发中某些场景必须要用到启动项目后立即执行方式的功能,如我们需要去初始化数据到redis缓存、设置策略工厂,或者启动后读取相应的配置等,主要聊聊实现立即执行的几种方法。

一、CommandLineRunner

实现CommandLineRunner接口 然后在run方法里面调用需要调用的方法即可,好处是方法执行时,项目已经初始化完毕,是可以正常提供服务的。

同时该方法也可以接受参数,可以根据项目启动时: java -jar demo.jar arg1 arg2 arg3 传入的参数进行一些处理。

@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(Arrays.toString(args));
}
}

二、 ApplicationRunner

这两者的实现方法一样,都是去继承相应的接口然后重写run方法即可,也都是SpringBoot框架所提供给我们的接口,也是项目中最常用的,比较灵活,有多个CommandLineRunner或ApplicationRunner实现类时可以通过@Order注解或实现Ordered接口重写getOrder方法实现按指定顺序执行。

@Order(1) //通过order注解指定执行顺序
@Component
public class CommandLineRunnerInit implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner 启动后执行方法...");
    }
}

ApplicationRunner 通过order注解指定执行顺序

@Order(2) //通过order注解指定执行顺序
@Component
public class ApplicationRunnerInit implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner 启动后执行方法...");
    }
}

ApplicationRunner 通过实现 Ordered 指定执行顺序

@Component
// 通过实现 Ordered 指定执行顺序
public class ApplicationRunnerInit implements ApplicationRunner, Ordered {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner 启动后执行方法...");
    }
    @Override
    public int getOrder() {
       return 0;
    }
}

这两者的不同其实就是run方法中所接收的参数类型不同,CommandLineRunner是对我们启动jar包时所传参数不进行处理,原样返回String类型给你,ApplicationRunner是封装成了ApplicationArguments参数,通过这个类型可以更方便的判断某些参数是否存在之类的。

三、JDK提供的@PostConstruct

@PostConstruct是JDK所提供的注解,使用该注解的方法会在服务器加载Servlet的时候运行。也可以在一个类中写多个方法并添加这个注解。

@Component
public class PostConstructTest {
    @PostConstruct
    public void inti1() {
        System.out.println("@PostConstruct 方法1执行...");
    }
    @PostConstruct
    public void inti2() {
        System.out.println("@PostConstruct 方法2执行...");
    }
}

三、其他方法

3.1 ApplicationContextAware

ApplicationContextAware 一般被用来获取applicationContext上下文信息,并且也可以启动时被执行

@Component
public class ApplicationContextAwareTest implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        System.out.println("ApplicationContextAware 方法执行...");
    }
}

3.2 ApplicationListener

ApplicationListener 事件监听,启动会执行多个监听器【会多次执行】,具体可了解Springboot服务启动过程。

@Component
public class ApplicationListenerTest implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("ApplicationListener 方法执行...");
    }
}

3.3 InitializingBean

InitializingBean 初始化启动后方法执行

@Component
public class InitializingBeanInit implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean 方法执行...");
    }
}

四、总结

以上多种初始执行方法日志输出为:

ApplicationContextAware 方法执行...
InitializingBean 方法执行...
    
@PostConstruct 方法1执行...
@PostConstruct 方法2执行...

ApplicationListener 方法执行...
ApplicationListener 方法执行...
ApplicationListener 方法执行...
ApplicationListener 方法执行...
    
ApplicationRunner 启动后执行方法...
CommandLineRunner 启动后执行方法...
    
ApplicationListener 方法执行...
ApplicationListener 方法执行...

存在多个ApplicationRunner、CommandLineRunner可以通过order指定其执行顺序,但其执行顺序没有ApplicationContextAware等高。但一般生产中使用ApplicationRunner、CommandLineRunner,因为其更加灵活。

到此这篇关于详解SpringBoot启动项目后执行方法的几种方式的文章就介绍到这了,更多相关SpringBoot启动后执行方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文