数据添加异步解析刷新大数据量redis (——)(二) SpringBoot之CommandLineRunner接口和ApplicationRunner接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/insis_mo/article/details/82255306

在spring boot应用中,我们可以在程序启动之前执行任何任务。为了达到这个目的,我们需要使用CommandLineRunnerApplicationRunner接口创建bean,spring boot会自动监测到它们。这两个接口都有一个run()方法,在实现接口时需要覆盖该方法,并使用@Component注解使其成为bean。CommandLineRunnerApplicationRunner的作用是相同的。不同之处在于CommandLineRunner接口的run()方法接收String数组作为参数,而ApplicationRunner接口的run()方法接收ApplicationArguments对象作为参数。当程序启动时,我们传给main()方法的参数可以被实现CommandLineRunnerApplicationRunner接口的类的run()方法访问。我们可以创建多个实现CommandLineRunnerApplicationRunner接口的类。为了使他们按一定顺序执行,可以使用@Order注解或实现Ordered接口。

CommandLineRunnerApplicationRunner接口的run()方法在SpringApplication完成启动时执行。启动完成之后,应用开始运行。CommandLineRunnerApplicationRunner的作用是在程序开始运行前执行任务或记录信息。

下面展示了如何在程序中使用CommandLineRunnerApplicationRunner

这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。下面我写两个简单的例子,来看一下这两个接口的实现。

CommandLineRunner

具体代码如下:

  1. import org.springframework.boot.CommandLineRunner;

  2. import org.springframework.stereotype.Component;

  3. /**

  4. * Created by zkn on 2016/8/12.

  5. */

  6. @Component

  7. public class TestImplCommandLineRunner implements CommandLineRunner {

  8.  
  9. @Override

  10. public void run(String... args) throws Exception {

  11.  
  12. System.out.println("这个是测试CommandLineRunn接口");

  13. }

  14. }

执行结果为:

ApplicationRunner接口

具体代码如下:

 
  1. package com.zkn.learnspringboot.runner;

  2. import org.springframework.boot.ApplicationArguments;

  3. import org.springframework.boot.ApplicationRunner;

  4. import org.springframework.stereotype.Component;

  5.  
  6. /**

  7. * Created by zkn on 2016/8/12.

  8. * 注意:一定要有@Component这个注解。要不然SpringBoot扫描不到这个类,是不会执行。

  9. */

  10. @Component

  11. public class TestImplApplicationRunner implements ApplicationRunner {

  12.  
  13.  
  14. @Override

  15. public void run(ApplicationArguments args) throws Exception {

  16. System.out.println(args);

  17. System.out.println("这个是测试ApplicationRunner接口");

  18. }

  19. }

执行结果如下:

@Order注解

如果有多个实现类,而你需要他们按一定顺序执行的话,可以在实现类上加上@Order注解。@Order(value=整数值)。SpringBoot会按照@Order中的value值从小到大依次执行。

CommandLineRunner和ApplicationRunner的执行顺序

在spring boot程序中,我们可以使用不止一个实现CommandLineRunnerApplicationRunner的bean。为了有序执行这些bean的run()方法,可以使用@Order注解或Ordered接口。例子中我们创建了两个实现CommandLineRunner接口的bean和两个实现ApplicationRunner接口的bean。我们使用@Order注解按顺序执行这四个bean。

CommandLineRunnerBean1.java

@Component
@Order(1)
public class CommandLineRunnerBean1 implements CommandLineRunner {
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 1");
    }
}
  •  

ApplicationRunnerBean1.java

@Component
@Order(2)
public class ApplicationRunnerBean1 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 1");
    }
}
  •  

CommandLineRunnerBean2.java

@Component
@Order(3)
public class CommandLineRunnerBean2 implements CommandLineRunner {
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 2");
    }
}
  • 1

ApplicationRunnerBean2.java

@Component
@Order(4)
public class ApplicationRunnerBean2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 2");
    }
} 
  • 1

输出结果为:

CommandLineRunnerBean 1
ApplicationRunnerBean 1
CommandLineRunnerBean 2
ApplicationRunnerBean 2 

Tips

如果你发现你的实现类没有按照你的需求执行,请看一下实现类上是否添加了Spring管理的注解(@Component)。

猜你喜欢

转载自blog.csdn.net/insis_mo/article/details/82255306