前言 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。
为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现。
order越小越先执行
1、order为1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.hlj.commandlinerunner.comhljcommandlinerunner.orlder;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Component @Order(value=1) public class MyStartupRunner1 implements CommandLineRunner { @Override public void run (String... args) throws Exception { System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<" ); } }
2、order为2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.hlj.commandlinerunner.comhljcommandlinerunner.orlder;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Component @Order(value=2) public class MyStartupRunner2 implements CommandLineRunner { @Override public void run (String... args) throws Exception { System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<" ); } }
3、启动SpringBoot
如果满意,请打赏博主任意金额,感兴趣的请下方留言吧。可与博主自由讨论哦