Spring项目启动时,如果需要初始化资源,可以实现ApplicationRunner或者CommandLineRunner,并且可以使用注解@Order指定他们的运行顺序。
示例代码:https://github.com/rickding/HelloJava/tree/master/HelloRunner
3个类文件:

RunnerApplication.java实现SpringBootApplication主程序:

HelloApplicationRunner.java实现ApplicationRunner:

HelloCommandLineRunner.java实现CommandLineRunner,并且指定@Order(1):

输出日志:

参考文章:https://blog.youkuaiyun.com/qq_28804275/article/details/80891941
本文介绍在Spring项目中如何通过实现ApplicationRunner或CommandLineRunner接口来控制启动时资源的初始化顺序,包括示例代码和具体实现方式。

906

被折叠的 条评论
为什么被折叠?



