本文共 659 字,大约阅读时间需要 2 分钟。
@
CommandLineRunner不是Spring框架原有的“宝贝”,它属于SpringBoot应用特定的回调扩展接口
: public interface CommandLineRunner { void run(String... args) throws Exception;}
CommandLineRunner需要大家关注的其实就两点:
1)所有CommandLineRunner的执行时点在SpringBoot应用的Application-Context完全初始化开始工作之后(可以认为是main方法执行完成之前最后一步)。
2)只要存在于当前SpringBoot应用的ApplicationContext中的任何Command-LineRunner,都会被加载执行(不管你是手动注册这个CommandLineRunner到IoC容器,还是自动扫描进去的)。
与其他几个扩展点接口类型相似,建议CommandLineRunner的实现类使用@org.springframework.core.annotation.Order进行标注
或者实现org.springframework.core.Ordered接口
,便于对它们的执行顺序进行调整,这其实十分重要,我们不希望顺序不当的CommandLineRunner实现类阻塞了后面其他CommandLineRunner的执行。
CommandLineRunner是很好的扩展接口,大家可以重点关注,我们在后面的扩展和微服务实践章节会再次遇到它。
转载地址:http://kltuf.baihongyu.com/