org.springframework.boot.ApplicationRunner.run 执行说明

ApplicationRunner是SpringBoot框架中的一个接口,用于在应用启动后执行任务。它接收ApplicationArguments对象,允许开发者处理启动参数。当应用启动时,Spring容器按Bean的加载顺序执行所有实现该接口的Bean。可以通过@Order注解调整执行顺序。这个接口常用于执行初始化或配置任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明

org.springframework.boot.ApplicationRunner 是 Spring Boot 框架中的一个接口,用于在 Spring Boot 应用程序启动后执行一些任务。它是一个函数式接口,只包含一个方法 void run(ApplicationArguments args),用于定义在应用程序启动后需要执行的任务。

ApplicationRunner 接口的执行原理如下:

在 Spring Boot 应用程序启动时,Spring 容器会加载所有实现了 ApplicationRunner 接口的 Bean。

Spring 容器会调用每个实现了 ApplicationRunner 接口的 Bean 的 run 方法,并将 ApplicationArguments 对象作为参数传入。

在 run 方法中,开发者可以编写需要在应用程序启动后执行的任务逻辑。

需要注意的是,ApplicationRunner 接口的执行顺序是按照 Bean 的加载顺序来执行的。如果需要控制多个 ApplicationRunner Bean 的执行顺序,可以使用 @Order 注解来指定执行顺序。

总之,ApplicationRunner 接口是 Spring Boot 中用于在应用程序启动后执行任务的接口,可以方便地实现一些初始化、启动和配置等任务。通过实现 ApplicationRunner 接口,并在 run 方法中编写任务逻辑,可以实现对应用程序的自定义扩展和定制。

Simply put

org.springframework.boot.ApplicationRunner is an interface in the Spring Boot framework that is used to perform some tasks after the Spring Boot application has started up. It is similar to the CommandLineRunner interface, but instead of accepting command-line arguments, it accepts the ApplicationArguments object, which contains the arguments passed to the application.

When a Spring Boot application starts up, it runs all the beans that implement the ApplicationRunner interface. To use ApplicationRunner , you need to implement the run method, which takes an ApplicationArguments object as a parameter. You can use this object to access the arguments passed to the application.

Here’s an example of how to use ApplicationRunner :

@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("Application started with arguments: " + Arrays.toString(args.getSourceArgs()));
        // Perform some tasks after the application has started up
    }
}

In this example, the MyApplicationRunner class implements the ApplicationRunner interface and overrides the run method. The run method simply prints out the arguments passed to the application and performs some tasks after the application has started up.

To use this class, you need to annotate it with @Component so that Spring Boot can detect it and run it after the application has started up.

17:28:05.025 [main] ERROR o.s.b.SpringApplication - [reportFailure,870] - Application run failed java.lang.IllegalStateException: Failed to execute ApplicationRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:811) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:798) at org.springframework.boot.SpringApplication.run(SpringApplication.java:350) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1359) at com.lianpingtech.RuoYiApplication.main(RuoYiApplication.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) Caused by: java.lang.NumberFormatException: null at java.lang.Long.parseLong(Long.java:552) at java.lang.Long.parseLong(Long.java:631) at com.lianpingtech.integration.task.CleanUdpLogListener.run(CleanUdpLogListener.java:54) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:808) ... 13 common frames omitted
最新发布
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值