SpringBoot启动流程之(SpringApplication#run()getRunListeners())
- getRunListeners(args)流程如下
1.第二大步SpringApplication的run()方法
public ConfigurableApplicationContext run(String...args){ StopWatch stopWatch=new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context=null; Collection<SpringBootExceptionReporter> exceptionReporters=new ArrayList<>(); configureHeadlessProperty(); //加载并实例化META-INF/spring.factories下面的SpringApplicationRunListener所对应的class集合 SpringApplicationRunListeners listeners=getRunListeners(args); //后面删掉了,因为这里不讲......
3.1.SpringApplicationRunListeners listeners = getRunListeners(args);
- getRunListeners()代码
private SpringApplicationRunListeners getRunListeners(String[]args){ Class<?>[]types=new Class<?>[]{SpringApplication.class,String[].class }; return new SpringApplicationRunListeners(logger, //注意入参SpringApplicationRunListener.class,会作为筛选META-INF/spring.factories的条件 getSpringFactoriesInstances(SpringApplicationRunListener.class,types,this,args)); }
以SpringApplicationRunListener.class为key值的META-INF/spring.factories全限定类如下
# Run Listeners org.springframework.boot.SpringApplicationRunListener=\ org.springframework.boot.context.event.EventPublishingRunListener
- 程序DEBUG截图如下
- 可见以SpringApplicationRunListener为入参,最终筛选的结果集便是EventPublishingRunListener
- SpringApplicationRunListener结构
- 可以看出SpringApplicationRunListener为一个接口,它的实现依赖于子类的继承
- 而EventPublishingRunListener继承了并且实现了所有抽象方法
当通过META-INF/spring.factorie实例化了EventPublishingRunListener对象之后,将其作为List转化为SpringApplicationRunListeners的入参,代码如下:
private SpringApplicationRunListeners getRunListeners(String[]args){ //这里的types会作为后面事件发布的参数 Class<?>[]types=new Class<?>[]{SpringApplication.class,String[].class }; //getSpringFactoriesInstances(SpringApplicationRunListener.class,types,this,args) //返回的实例集合转为SpringApplicationRunListeners的入参,在实例化集合创建时同时完成了ApplicationListener的广播处理 return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(SpringApplicationRunListener.class,types,this,args)); }
- 加载全类名
private<T> Collection<T> getSpringFactoriesInstances(Class<T> type,Class<?>[]parameterTypes,Object...args){ ClassLoader classLoader=getClassLoader(); // 通过Set集合去除同名class,根据type属性加载META-INF/spring.factorie下的class全类名 Set<String> names=new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type,classLoader)); //实例化class集合,前面讲过,这里就不再说了 List<T> instances=createSpringFactoriesInstances(type,parameterTypes,classLoader,args,names); AnnotationAwareOrderComparator.sort(instances); return instances; }
- 将ApplicationListener交给广播处理
public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered { private final SpringApplication application; private final String[] args; //广播器 private final SimpleApplicationEventMulticaster initialMulticaster; //application为反射创建对象的SpringApplication对象 public EventPublishingRunListener(SpringApplication application, String[] args) { this.application = application; this.args = args; this.initialMulticaster = new SimpleApplicationEventMulticaster(); //获取在构造SpringApplication时创建的ApplicationListener并把他们交给广播处理 for (ApplicationListener<?> listener : application.getListeners()) { this.initialMulticaster.addApplicationListener(listener); } }