1. 方式一
/**
* Desc: 系统启动完可以做一些业务操作
*/
@Component
//如果有多个runner需要指定一些顺序
@Order(1)
public class SimosApplicationRunner implements ApplicationRunner {
@Autowired
SystemInitService systemInitService;
@Override
public void run(ApplicationArguments args) throws Exception {
systemInitService.systemInit();
}
}
2. 方式二
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(2)
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
System.out.println("====================MyCommandLineRunner================");
System.out.println("order值: 2");
System.out.println("原始参数: " Arrays.asList(args));
System.out.println("=======================================================");
}
本文由博客一文多发平台 OpenWrite 发布!
本文介绍了在SpringBoot应用中,通过实现ApplicationRunner和CommandLineRunner接口来执行启动后的业务操作。这两种方式均可在系统初始化完成后执行特定任务,且可通过@Order注解定义执行顺序。
5901

被折叠的 条评论
为什么被折叠?



