天天看点

SpringBoot源码分析之CommandLineRunner、ApplicationRunner

我们在之前的文章中简单的说过SpringBoot的CommandLineRunner和ApplicationRunner这两个接口

接口。在org.springframework.boot.SpringApplication#run()这个方法中有这样一段代码:

方法内容如下:

SpringBoot的注释中说,在上下文刷新完之后调用这个方法。在调用这个方法的时候Spring容器已经启动完

成了。这里的context的真正对象是:AnnotationConfigEmbeddedWebApplicationContext,这个类贯

穿着SpringBoot的整个启动过程。我们看一下callRunners这个方法的内容:

callRunner方法的内容就很简单了直接调用run方法。

ApplicationRunner和CommandLineRunner的区别就是run方法参数不同,ApplicationRunner中run方法

的参数是ApplicationArguments,CommandLineRunner中run方法的参数是String类型的可变参数。。