TOC
前言
CommandLineRunner
接口在容器启动成功后最后一步调用,常用于应用程序启动后初始化操作,其在整个应用生命周期内只会执行一次。
具体应用
实现CommandLineRunner接口
- Runner.java
import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;@Componentpublic class Runner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {/*** TODO:*/System.out.println(" Runner run ");}}
-
CommandLineRunner
会在
Spring Boot
容器加载之后执行
CommandLineRunner实现类的执行顺序
- 使用
@Order
注解定义
CommandLineRunner
的执行顺序
import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Component@Order(1)public class RunnerOne implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {/*** TODO:*/System.out.println(" RunnerOne run ");}}
import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Component@Order(2)public class RunnerTwo implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {/*** TODO:*/System.out.println(" RunnerTwo run ");}}
– End -﹀﹀﹀梦想是咸鱼关注一下吧