AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
# 23.8. 使用ApplicationRunner或CommandLineRunner ### 23.8. 使用ApplicationRunner或CommandLineRunner 如果需要在`SpringApplication`启动后执行一些特殊的代码,你可以实现`ApplicationRunner`或`CommandLineRunner`接口,这两个接口工作方式相同,都只提供单一的`run`方法,该方法仅在`SpringApplication.run(…)`完成之前调用。 `CommandLineRunner`接口能够访问string数组类型的应用参数,而`ApplicationRunner`使用的是上面描述过的`ApplicationArguments`接口: ``` 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`注解。