springboot的项目都会有一个启动类,这个是我们项目的入口,但是当我们想让工程启动的时候做一些其他事情,就不能用原有的启动类了,我们看一下run方法的源码:
public ConfigurableApplicationContext run(String... args) {
// 创建计时器
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
// 设置属性
this.configureHeadlessProperty();
// spring中监听器的使用,这些对象想创建,就得知道监听器的全路径
// 会从spring.factory中读取
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
// 初始化默认应用参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 根据监听器和默认的参数 来准备spring所需要的环境
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
// 打印出banner
Banner printedBanner = this.printBanner(environment);
// 创建应用上下文
context = this.createApplicationContext();
// 异常报告
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
// 准备应用上下文
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 刷新上下文
this.refreshContext(context);
// 刷新
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
// 执行callRunners, 支持自定义run方法
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
发现了这个方法try模块的最后一行的callRunners方法,是支持自定义的run方法,再点进去看看
private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
Iterator var4 = (new LinkedHashSet(runners)).iterator();
while(var4.hasNext()) {
Object runner = var4.next();
if (runner instanceof ApplicationRunner) {
this.callRunner((ApplicationRunner)runner, args);
}
if (runner instanceof CommandLineRunner) {
this.callRunner((CommandLineRunner)runner, args);
}
}
}
看到了ApplicationRunner,CommandLineRunner这两个类,这两个类就是支持自定义run方法的
具体方法如下:
启动类实现ApplicationRunner或者CommandLineRunner接口,并把需要执行的代码逻辑等重写进run方法内
package com.jym.demo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author jym
*/
@SpringBootApplication()
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
new SpringApplication(DemoApplication.class).run();
}
public void run(String... args) throws Exception {
System.out.println("jymQWQ");
}
}
这样在启动项目的时候,我们需要执行的代码就执行了