从中可以看到,这个是SpringBoot中的一个接口,他是在Spring环境都建起了后,一个一个的调用,都是在主线程中调用,如下:
事例代码如下:
DisposeOne.java
@Component
public class DisposeOne implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(Thread.currentThread() + ":" + "DisposeOne wants to do sth");
}
}
DisposeTwo.java
@Component
public class DisposeTwo implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(Thread.currentThread() + ":" + "DisposeTwo wants to do sth");
}
}
这里可以看到,主函数依次进行2个Component中的run。
那么这个有什么用呢,当SpringBoot上下文环境构建完成后,我们可以使用这个方式,调用自己的逻辑业务。也就是使得代码更加优雅,这里要注意,因为都是主线程依次进行,不能使用死循环,这样会把其他地方逻辑业务搞死,常驻的还是需要用新线程去搞。
源码打包下载地址:
https://github.com/fengfanchen/Java/tree/master/CommandLineRunnerTest