CommandLineRunner和ApplicationRunner比较

在springboot构建的项目启动后,想让程序自动执行一些任务,我们可以使用CommandLineRunner或ApplicationRunner实现。首先,我们来看看简单的源码。

/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 * <p>
 * If you need access to {@link ApplicationArguments} instead of the raw String array
 * consider using {@link ApplicationRunner}.
 *
 * @author Dave Syer
 * @see ApplicationRunner
 */
@FunctionalInterface
public interface CommandLineRunner {

	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;

}

简单吧,就一个函数式接口。来看看注释:在一个SpringApplication项目中,此接口被用于标明一个实现类的bean应执行run方法。在同一个应用上下文,可以定义多个CommandLineRunner实现类的bean,并且这些bean可以使用Ordered接口或@Order注释进行排序。如果需要访问ApplicationArguments而不是原始的String数组,请考虑使用ApplicationRunner。怎么样,我翻译的还行吧。:D 现在,来看看官网https://spring.io

直接搜索CommandLineRunner就行啦。查询结果如下:

选择第一个点进去,可以看到javadoc文档。当然了,你可以使用Google的翻译成中文(右击网页)。现在,你可以对比一下翻译结果和我的翻译结果,哪个更容易理解。我想你心中已有答案了。:D

接下来,我们看看该如何使用CommandLineRunner。使用Intellij IDEA构建一个Maven的Web项目,这很简单,动手试试。

如果你已经完成了,那我们继续接下来的学习啦。在你的启动类所在包下新建一个CommandLineRunnerStudy 类,并且实现run接口(Ctrl + o 快捷键),简单地打印一句"Hello CommandLineRunner!"。注意:使用@Component注解,这样该实现类才能作为一个bean注入到spring Ioc 容器中。

/**
 * @author Cheng Jun
 * @version 1.0
 * @Description:
 * @date 2019/8/5 17:55
 */
@Component
public class CommandLineRunnerStudy implements CommandLineRunner {
    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    @Override
    public void run(String... args) throws Exception {
        System.out.println("Hello CommandLineRunner!");
    }
}

好了,大功告成!启动你的程序,可以看到控制台打印出了"Hello CommandLineRunner!"。现在让我们再来看看,String... args参数该如何使用。这个可变参数就是启动类中的String[] args,也就是我们传入启动类的参数可以直接在该方法中使用。那么该如何向启动类的main方法传入参数呢?在Intellij IDEA中我们可以这样做,点击菜单栏Run->Edit Configurations->Environment(默认为 collapse all),在Program arguments中输入你想设置的数组。然后在CommandLineRunnerStudy的run方法中添加"System.out.println(args[0]);"就可以打印出来了。

恭喜你,获得了CommandLineRunner的使用技巧。ApplicationRunner的使用和CommandLineRunner相同,只是在方法的参数有些区别。如果你想更进一步学习,我推荐你看看这篇博客https://blog.youkuaiyun.com/zongzhankui/article/details/78681942

如果对你有帮助,请点赞关注博主!

### Spring Boot 中 CommandLineRunner ApplicationRunner 的执行顺序 在 Spring Boot 应用程序启动过程中,`CommandLineRunner` `ApplicationRunner` 是两个常用的接口,用于在应用完全加载并运行后执行特定的任务。两者的主要区别在于参数的处理方式以及执行顺序的一致性。 #### 接口对比 - **CommandLineRunner**: 提供一个字符串数组作为参数,允许直接访问命令行传递的原始参数[^1]。 - **ApplicationRunner**: 使用更高级别的 `ApplicationArguments` 对象,该对象不仅包含原始参数列表,还提供了解析后的选项非选项参数[^3]。 #### 执行顺序 无论定义了多少个 `CommandLineRunner` 或 `ApplicationRunner` 实现类,Spring Boot 都会按照以下规则决定它们的执行顺序: 1. 如果某个实现类标注了 `@Order` 注解,则优先级较高的(数值较小)先被执行。 2. 若未指定 `@Order`,则默认情况下,所有 `CommandLineRunner` 实现会在所有 `ApplicationRunner` 实现之前执行。 3. 在同一类型的多个实现之间(例如多个 `CommandLineRunner`),如果没有显式的 `@Order` 定义,默认行为是由容器内部管理的自然顺序(通常是声明或注册的顺序)。这种顺序可能依赖于配置文件、自动装配或其他上下文因素[^4]。 以下是具体示例代码展示两种接口的行为及其执行次序: ```java // Example of CommandLineRunner implementation with an Order annotation. import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component @Order(1) public class MyFirstCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Executing First Command Line Runner..."); } } // Another CommandLineRunner without explicit ordering. @Component public class MySecondCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Executing Second Command Line Runner..."); } } // An example using the ApplicationRunner interface instead. import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component @Order(2) public class MyAppRunner implements ApplicationRunner { @Override public void run(org.springframework.boot.ApplicationArguments args) throws Exception { System.out.println("Running App Runner Implementation..."); } } ``` 当此应用程序启动时,预期输出将是这样的顺序: 1. 输出来自具有最低 `@Order` 值的第一个 `CommandLineRunner`. 2. 然后再依次打印其他无特别排序指示或者较高 `@Order` 数字的 `CommandLineRunner`s 结果. 3. 最终才会轮到任何已配置好的 `ApplicationRunner`,同样遵循其自身的 `@Order` 设置. 因此,在上面的例子中,实际日志应该如下所示: ``` Executing First Command Line Runner... Executing Second Command Line Runner... Running App Runner Implementation... ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值