SpringBoot之ApplicationRunner、CommandLineRunner

简介:

CommandLineRunner、ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动)。

使用:

CommandLineRunner的使用,编写一个类去实现CommandLineRunner接口,并覆写run方法,即可接入Spring观察者模式的回调功能,完成自己在容器启动成功之后想做的事情。

ApplicationRunner 和CommandLineRunner的区别只是在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理。ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。

11772383-c7e63b47875b6874.png
CommandLineRunner


11772383-bdeaa4a1a748a032.png
ApplicationRunner

如上图,ApplicationRunner 和 CommandLineRunner均支持多个监听,所以可以使用@Order注解去指定监听器执行链的执行顺序,它会按@Order注解指定的顺序依次执行你的回调。

猜你喜欢

转载自blog.csdn.net/weixin_33739541/article/details/87259400