如果你需要在你的SpringBoot启动完成之后实现一些功能,那么可以通过创建class实现ApplicationRunner和CommandLineRunner来完成:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14@Component public class ApplicationRunnerTest implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("========>> ApplicationRunner.run"); for (String sourceArg : args.getSourceArgs()) { System.out.println(sourceArg); } for (String optionName : args.getOptionNames()) { System.out.println(optionName + " = " + args.getOptionValues(optionName)); } System.out.println("========>> End"); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11@Component public class CommandLineRunnerTest implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("========>> CommandLineRunner.run"); for (String sourceArg : args) { System.out.println(sourceArg); } System.out.println("========>> End"); } }
如果你定义了多个ApplicationRunner或者CommandLineRunner,并想要控制他们执行的先后顺序,可以让你定义的class实现org.springframework.core.Ordered接口,或者直接注解@Order
转载于:https://www.cnblogs.com/LOVE0612/p/9890718.html
最后
以上就是热心毛衣最近收集整理的关于SpringBoot的ApplicationRunner和CommandLineRunner的全部内容,更多相关SpringBoot内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复