> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/23.-springapplication/23.8.-using-the-applicationrunner-or-commandlinerunner.md).

# 23.8. 使用ApplicationRunner或CommandLineRunner

如果需要在`SpringApplication`启动后执行一些特殊的代码，你可以实现`ApplicationRunner`或`CommandLineRunner`接口，这两个接口工作方式相同，都只提供单一的`run`方法，该方法仅在`SpringApplication.run(…)`完成之前调用。

`CommandLineRunner`接口能够访问string数组类型的应用参数，而`ApplicationRunner`使用的是之前描述过的`ApplicationArguments`接口：

```java
import org.springframework.boot.*
import org.springframework.stereotype.*

@Component
public class MyBean implements CommandLineRunner {

    public void run(String... args) {
        // Do something...
    }

}
```

如果某些定义的`CommandLineRunner`或`ApplicationRunner` beans需要以特定的顺序调用，你可以实现`org.springframework.core.Ordered`接口或使用`org.springframework.core.annotation.Order`注解。
