1. 实现ApplicationRunner接口
// runner/BootApplicationRunner.java
@Component
//@Order(2)
public class BootApplicationRunner implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(BootApplicationRunner.class);
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("This is ApplicationRunner....");
}
}
2.实现CommandLineRunner接口
// runner/BootCommandLineRunner.java
@Component
//@Order(1)
public class BootCommandLineRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(BootApplicationRunner.class);
@Override
public void run(String... args) throws Exception {
logger.info("this is CommandLineRunner...");
}
}
区别:传入的参数不同,ApplicationRunner的优先级高于CommandLineRunner。
可以使用@Order(1)注解改变优先级,数字越小,优先级越高