首先是SpringApplication.run
方法,方法拿到一個 primary source
然後解析加載配置(就是那個傳進來的 App.class)。
然後創建了個SpringApplication
, 創建的時候會檢測這個是什麼類型的項目,this.webApplicationType = WebApplicationType.deduceFromClasspath();
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
把參數傳進去,然後run()
, 根據項目類型創建ConfigurableApplicationContext
,
public ConfigurableApplicationContext run(String... args) {
// 啟動計時器
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
// 這個東西用來管理多個listener,在創建的時候,從 META-INF/spring.factories拿到要創建的listener名字,創建實例
SpringApplicationRunListeners listeners = getRunListeners(args);
// 啟動listener
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
// 創建運行參數
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
// 輸出開頭那段Spring boot 什麼什麼的
Banner printedBanner = printBanner(environment);
// 創建上下文
context = createApplicationContext();
// 異常報告
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//前置上下文配置
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
// 刷新上下文
refreshContext(context);
afterRefresh(context, applicationArguments);
// 計時完畢
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
listeners 是拿到這些名字來創建東西的:
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
也就是說,我們是可以自定義 Listener 然後,監聽相應的 Event
只要實現ApplicationListener
/**
* @author dqn
* created at 2019/2/10 19:20
*/
public class MyListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 監聽ApplicationStartingEvent
if (event instanceof ApplicationStartedEvent) {
System.out.println("ApplicationStartedEvent listened");
}
// 監聽ApplicationEnvironmentPreparedEvent
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
System.out.println("ApplicationEnvironmentPreparedEvent listened");
}
// 監聽ApplicationPreparedEvent
else if (event instanceof ApplicationPreparedEvent) {
System.out.println("ApplicationPreparedEvent listened");
}
// 監聽ApplicationReadyEvent
else if (event instanceof ApplicationReadyEvent) {
System.out.println("ApplicationReadyEvent listened");
}
// 監聽ApplicationFailedEvent
else if (event instanceof ApplicationFailedEvent) {
System.out.println("ApplicationFailedEvent listened");
}
}
}
在META-INF/spring.factories
加上
org.springframework.context.ApplicationListener=\
me.dqn.listener.MyListener
即可。當然也可以手動添加:
public static void main(String[] args) {
SpringApplication application = new SpringApplication(App.class);
application.addListeners(new MyListener());
application.run(args);
}