在啓動流程中,會出發許多ApplicationEvent。這時會調用對應的listener的onApplicationEvent方法。ApplicationEvent時觀察者模式, spring
(1) 實體繼承ApplicationEventapp
(2) 注入的Listener 實現接口ApplicationListener, 觸發後的處理邏輯在onApplicationnEvent中ide
(3) 事件發起方經過ApplicationContext 實例的publishEvent方法將 (1)的實體傳入(2)this
SpringBoot 啓動時的入口方法SpringApplication.run 內部實現爲url
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return new SpringApplication(primarySources).run(args); }
第一步 new SpringApplication 內部會注入系統Listener 具體的listeners參見 https://blog.csdn.net/zl1zl2zl3/article/details/79765725spa
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
第二步 調用 run(args) 啓動Listener.net
SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting();
第二步 2.1 如何獲取監聽器:在getRunListeners中會讀取配置文件,加載的是Springboot META-INF/spring.factories 中的 org.springframework.boot.context.event.EventPublishingRunListener, 並生成"事件發佈啓動監聽器"的工廠實例code
private SpringApplicationRunListeners getRunListeners(String[] args) { Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class }; return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances( SpringApplicationRunListener.class, types, this, args)); } private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type) { return getSpringFactoriesInstances(type, new Class<?>[] {}); } private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // 使用Set確保的字符串的惟一性 Set<String> names = new LinkedHashSet<String>( SpringFactoriesLoader.loadFactoryNames(type, classLoader));// 1.載入工廠名稱集合 List<T> instances = createSpringFactoriesInstances(type, parameterTypes,// 2.建立工廠實例 classLoader, args, names); AnnotationAwareOrderComparator.sort(instances);// 排序 return instances; }
META-INF/spring.factoriesblog
# Run Listeners 這裏呢,看這裏!!!! org.springframework.boot.SpringApplicationRunListener=\ org.springframework.boot.context.event.EventPublishingRunListener
第二步 2.2 啓動監聽 starting()排序
SpringApplicationRunListeners listeners = this.getRunListeners(args); //若是有監聽器監聽啓動事件,則執行對應的動做 listeners.starting();
當前監聽是EventPublishingRunListener,starting 方法以下
public void starting() { this.initialMulticaster .multicastEvent(new ApplicationStartedEvent(this.application, this.args)); } public void multicastEvent(ApplicationEvent event) { multicastEvent(event, resolveDefaultEventType(event)); } public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) { ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event)); for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) { Executor executor = getTaskExecutor(); if (executor != null) { executor.execute(new Runnable() { @Override public void run() { invokeListener(listener, event); } }); } else { invokeListener(listener, event); } } }