有两个函数接口类都可以实现
1.ApplicationRunner接口
源码
package org.springframework.boot;
@FunctionalInterface
public interface ApplicationRunner {
void run(ApplicationArguments args) throws Exception;
}
复制代码
使用方法
使用@SpringBootApplication
注释的ApplicationMain启动类实现ApplicationRunner
接口,并实现run方法即可 在main方法将spring启动完成后会执行run方法中的程序
@SpringBootApplication
public class BootTestApplication implements ApplicationRunner{
public static void main(String[] args) {
SpringApplication.run(BootTestApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(“ApplicationRunner.run”);
}
}
复制代码
2.CommandLineRunner接口
源码
package org.springframework.boot;
@FunctionalInterface
public interface CommandLineRunner {
void run(String… args) throws Exception;
}
复制代码
使用方法
其实CommandLineRunner
和ApplicationRunner
使用方法差不多
使用@SpringBootApplication<