0%

springBoot启动时执行

文章字数:88,阅读全文大约需要1分钟

开发的时候需要项目启动后执行一些初始化功能,soringBoot中可以添加一个model并实现CommandLineRunner接口,实现功能的代码放在实现的run方法中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.springboot.sample.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component //作为bean加入spring
@Order(value=2)//多个启动任务的执行顺序
public class MyStartupRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行 2222 <<<<<<<<<<<<<");
}
}