实现CommandLineRunner接口
package org.springframework.boot;
@FunctionalInterface
public interface CommandLineRunner {
void run(String... args) throws Exception;
}
这是一个接口,用户可以自定义实现该接口,具体实现run方法
任何在上下文容器之内的bean都可以实现run方法
如果在上下文中,存在多个该接口实现类,可以通过@order注解,指定加载顺序
案例demo1
@Component
@Slf4j
@Order(1)
public class InitData implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("加载1");
}
}
案例demo2
@Component
@Slf4j
@Order(2)
public class InitData implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("加载2");
}
}
启动结果:加载1先打印,按照order从小到大按顺序执行