有时候我们需要在应用启动或者重启后做一些工作,比如删除一些临时文件或者Redis
中的缓存,这在没有使用Spring Boot
之前,在Spring框架下是通过ApplicationListener
监听器来实现的,在Spring Boot
下也有一些办法,可以通过使用CommandLineRunner
和ApplicationRunner
接口来实现。
在这两个接口中都有run()方法,在Spring Boot程序启动后会检测程序中是否有CommandLineRunner
和ApplicationRunner
接口的实例,如果存在,则会执行对应实现类中的run()方法,而且只执行一次。
Spring Boot程序启动过程中会实例化
ApplicationContext
,然后创建所有的Bean。
如果存在多个CommandLineRunner
和ApplicationRunner
接口的实例,那么它们的执行顺序可以通过@Order
注解和Ordered
接口来标识,数字越小,优先级越高。
看如下实例:
TaskRunner类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
StartupRunner类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
然后在Application主类中定义这两个bean,由于@SpringBootApplication
注解中包含@Configuration
注解,所以@Bean注解在其中也会起作用,如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
输出如下:
可以看出由于StartupRunner
类优先级较高所以先运行,TaskRunner
后运行。
关于CommandLineRunner
和ApplicationRunner
的区别,在官方文档中是这么说的:
Interface used to indicate that a bean should run when it is contained within
a SpringApplication. Multiple CommandLineRunner beans can be defined
within the same application context and can be ordered using theOrdered
interface or@Order
annotation.
If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner.
其实没有很大区别,如果想要更详细地获取命令行参数,那就使用ApplicationRunner
接口。