CommandLineRunner是一个带有运行方法的简单弹簧引导接口.Spring引导启动后将自动调用实现CommandLineRunner接口的所有豆的运行方法。
命令行跑步者在加载应用程序上下文之后以及Spring应用程序运行方法完成之前执行,相当于你的应用的初始化过程,一般用来实现一些数据预先加载或预先处理。
@SpringBootApplication & lt; b> public& lt; b> classDemoApplication实现CommandLineRunner { & lt; b> private& lt; b> final日志记录器=LoggerFactory.getLogger (DemoApplication灵活;b> class); & lt; b> public& lt; b> static& lt; b> voidmain (String参数){ SpringApplication.run (DemoApplication灵活;b> class, args); } @Override & lt; b> public& lt; b> void运行(字符串…字符串){抛出异常 … } }
上面的运行方法参数是命令行参数,使用java jar启动这个应用的命令行参数。
如果有多个命令行运行器,可以进行排序:
@ component @Order (1) & lt; b> public& lt; b> classAnotherDatabaseLoader实现CommandLineRunner { @ component @Order (2) & lt; b> public& lt; b> classDataLoader实现CommandLineRunner {
另外一种在主应用的写法:
@SpringBootApplication & lt; b> public& lt; b> classUnsplashApplication { & lt; b> public& lt; b> static& lt; b> voidmain (String参数){ SpringApplication.run (UnsplashApplication灵活;b> class, args); } @ bean CommandLineRunner跑步者(){ & lt; b> returnargs→{ System.out.println (& lt; font>“CommandLineRunner UnsplashApplication类…”& lt;/font> & lt; font>); }; } } & lt;/font>
以上所述是小编给大家介绍的弹簧引导命令行运行器的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!