在使用SpringBoot构建项目时,我们通常有一些预先数据的加载。那么SpringBoot提供了一个简单的方式来实现–CommandLineRunner。 CommandLineRunner是一个接口,我们需要时,只需实现该接口就行。如果存在多个加载的数据,我们也可以使用@Order注解来排序。 案例: 分别定义了一个数据加载类 MyClass ,排序为2 @Component @Order(value = 2) public class MyClass implements CommandLineRunner{ @Override public void run(String... strings) throws Exception { } 以上该方法在springboot启动时就会预先执行,切如果有多个CommandLineRunner实现类时执行加载的顺序与Order排序有关(order值越小,执行越靠前)! 参考原文:https://blog.csdn.net/ruben95001/article/details/78340700
springboot中类数据预加载之CommandLineRunner
猜你喜欢
转载自blog.csdn.net/qq_16739081/article/details/88783294
今日推荐
周排行