深入理解 Spring Boot 的 ApplicationRunner 接口
warning:
这篇文章距离上次修改已过208天,其中的内容可能已经有所变动。
Spring Boot 的 ApplicationRunner
和 CommandLineRunner
接口可以用来在 Spring Boot 应用程序启动并完成所有 Spring 容器初始化之后,执行自定义的启动逻辑。
ApplicationRunner
和 CommandLineRunner
接口都只包含一个方法,这个方法将在 Spring 应用启动的最后阶段被调用。
ApplicationRunner
接口的 run
方法提供了一个 ApplicationArguments
参数,该参数可以用来访问应用程序的参数。
CommandLineRunner
接口的 run
方法提供了一个 String...
参数,该参数是一个包含传递给应用程序的命令行参数的数组。
以下是一个使用 ApplicationRunner
接口的简单示例:
以下是一个使用 CommandLineRunner
接口的简单示例:
这两个接口可以用于执行一些需要在 Spring 容器完全启动之后才想执行的操作,例如数据库数据填充、缓存预热等。
评论已关闭