问题背景
如果希望在SpringBoot应用启动时进行一些初始化操作,可以选择SpringBoot提供了一个简单的方式来实现此类需求:选择使用CommandLineRunner来进行处理
只需要实现CommandLineRunner接口,并且把对应的bean注入容器。把相关初始化的代码重新到需要重新的方法中
这样就会在应用启动的时候执行对应的代码
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Component @Order(value = 1) public class TestCommandLineRunner implements CommandLineRunner {
@Override public void run(String... args) throws Exception { System.out.println("这是一个测试CommandLineRunner的测试方法"); System.out.println("程序初始化中......"); } }
|
注意
启动CommandLineRunner的执行其实是整个应用启动的一部分,项目是在CommandLineRunner执行完成之后才启动完成的
如果CommandLineRunner执行中的操作影响到了主线程,可以重新开启一个线程,让CommandLineRunner单独去做我们想要做的操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @Component @Order(value = 1) public class TestCommandLineRunner implements CommandLineRunner { @Override public void run(String... strings){ new Thread(){ public void run() { int i = 0; while (true) { i++; try { Thread.sleep(10000); System.out.println("过去了10秒钟……,i的值为:" + i); } catch (InterruptedException e) { e.printStackTrace(); } if (i == 4) { throw new RuntimeException(); } continue; } } }.start(); } }
|
参考链接