ApplicationRunner和CommandLineRunner

在SpringBoot中org.springframework.boot下提供了两个接口来实现其需求:
ApplicationRunner和CommandLineRunner
这两个接口中都提供了一个run方法,在实现接口时进行覆盖,他们会在容器启动完成后自动执行其中的内容。

90237-lgt7b73m78.png

ApplicationRunner和CommandLineRunner的区别

两者作用是一样的,区别在与前者run方法参数为ApplicationArguments**对象,是对原始参数做了封装,而后者为原始String数组。注:这些参数都是传递给main方法的参数。

package org.springframework.boot;
@FunctionalInterface
public interface ApplicationRunner {
    void run(ApplicationArguments args) throws Exception;
}
package org.springframework.boot;
@FunctionalInterface
public interface CommandLineRunner {
    void run(String... args) throws Exception;
}

执行顺序

在SpringBoot中,不止一个Bean可以实现ApplicationRunner或CommandLineRunner,为了能控制其run方法的执行顺序,支持使用@Order注解来进行执行排序。注:在均实现ApplicationRunner和CommandLineRunner时,ApplicationRunner的run方法会先执行。

标签: ApplicationRunner和CommandLineRunner接口


阿里云优惠主机

添加新评论

免责声明
本博客部分内容来自于互联网,不代表作者的观点和立场,如若侵犯到您的权益,请联系[email protected]。我们会在24小时内进行删除。