ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
如果在SpringApplication启动后需要运行某些特定代码,则可以实现ApplicationRunner或CommandLineRunner接口。 两个接口以相同的方式工作,并提供单个run方法,该方法在SpringApplication.run(...)完成之前调用。 CommandLineRunner接口提供对应用程序参数的访问,作为简单的字符串数组,而ApplicationRunner使用前面讨论的ApplicationArguments接口。 以下示例显示了带有run方法的CommandLineRunner: ``` import org.springframework.boot.*; import org.springframework.stereotype.*; @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } } ``` 如果定义了必须以特定顺序调用的多个CommandLineRunner或ApplicationRunner bean,则还可以实现org.springframework.core.Ordered接口或使用org.springframework.core.annotation.Order注解。