CommandLineRunner接口
如果希望在SpringBoot应用启动时进行一些初始化操作,可以选择使用CommandLineRunner来进行处理。
我们只需要实现CommandLineRunner接口,把实现类注入到spring中。在重写的方法run中写相关的代码。
这样就会在应用启动的时候执行对应的代码。
package com.liyadong.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class TestRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("程序初始化");
}
}