springboot2.0.6如何启动监听器

  介绍

这篇文章给大家介绍springboot2.0.6如何启动监听器,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

解析SpringApplication的运行方法解析

 public  ConfigurableApplicationContext 运行(字符串…,args), {
  ,,…//省略
  ,,,//,获取一个运行监听器,主要监听SpringApplication对象,(内部只有一个EventPublishingRunListener)
  ,,SpringApplicationRunListeners  listeners =, getRunListeners (args);
  ,,,//调用监听器的启动,当SpringApplication对象的运行方法刚启动的时候(依靠SimpleApplicationEventMulticaster)
  ,,listeners.starting ();
  ,,…//省略
  }

1。SpringApplicationRunListeners

,,,, SpringApplicationRunListeners是一个集合类,内部包含一个日志和包含SpringApplicationRunListener的列表。而SpringApplicationRunListener主要是监听SpringApplication对象的,里面的方法都定义了在何时调用SpringApplicationRunListener的各种方法。

,,,下面的每一个方法SpringApplicationRunListener都把其包装成一个事件,在春容器还未成功refreshed 之前都是使用SimpleApplicationEventMulticaster去寻找对该事件感兴趣的ApplicationListener,然后调用其onApplicationEvent方法

<李>

开始:当SpringApplication对象的运行方法刚启动的时候(依靠SimpleApplicationEventMulticaster)

<李>

environmentPrepared:在环境准备但是春容器还未创建的时候(依靠SimpleApplicationEventMulticaster)

<李>

contextPrepared:当春容器已经创建且准备好了,(目前是空的实现)

<李>

contextLoaded:当春容器已经加载且未刷新.load就是将我们的primaryClass注册到春容器中,(依靠SimpleApplicationEventMulticaster)同时将之前获取到的ApplicationListener都加入到春容器中,此时如果ApplicationListener还是ApplicationContextAware的也要调用其setApplicationContext方法。

<李>

开始:春容器已经刷新过且应用已经启动,但是CommandLineRunners和ApplicationRunners还未调用,直接通过春容器自己发送(因为ApplicationListener已经加入春容器)

<李>

运行:我们已经调用了CommandLineRunners,直接通过春容器自己发送(因为ApplicationListener已经加入春容器)

<李>

失败:当异常发生的时候就调用这个,如果春容器没有加载或者没有激活就使用SimpleApplicationEventMulticaster,否则还是依靠春容器自己

2。获取SpringApplicationRunListeners实例源码分析

1。getRunListeners

<>以前public  class  SpringApplication  {   ,,,//,获取监听   ,,private  SpringApplicationRunListeners  getRunListeners (String [], args), {   ,,,,,,,//,定义类数组   ,,,,,Class<?在[],types =, new  Class<?在[],{,SpringApplication.class, String [] .class };   ,,,,,,,//,创建SpringApplicationRunListeners对象   ,,,,,return  new  SpringApplicationRunListeners(记录器,getSpringFactoriesInstances (   ,,,,,,,,,,,SpringApplicationRunListener.class,,,,,, args));   ,,}   }

默认情况下,getRunListeners方法从春天。工厂文件中找出关键为SpringApplicationRunListener的类有:

,,,, org.springframework.boot.context.event.EventPublishingRunListener

这里我们看到了一个熟悉的方法getSpringFactoriesInstances (SpringApplicationRunListener。类、类型,args),前面的博文我们已经详细介绍过该方法是怎么一步步的获取到meta - inf/spring。工厂中的指定的键的值,获取到以后怎么实例化类的(参考)。执行获取的值如下图

 springboot2.0.6如何启动监听器

,,从上图调试结果,我们可以看到我们获取到了一个监听器EventPublishingRunListener,该监听器是春容器的启动监听器.listeners.starting()方法开启了监听事件

2只SpringApplicationRunListener的实现类EventPublishingRunListener

 public  class  EventPublishingRunListener  implements  SpringApplicationRunListener, Ordered  {
  ,,private  final  SpringApplication 应用;
  ,,private  final  String [], arg游戏;
  ,,private  final  SimpleApplicationEventMulticaster  initialMulticaster;
  ,,,//,加载监听器类
  ,,public  EventPublishingRunListener (SpringApplication 应用程序,String [], args), {
  ,,,,,this.application =,应用程序;
  ,,,,,this.args =, arg游戏;
  ,,,,,this.initialMulticaster =, new  SimpleApplicationEventMulticaster ();
  ,,,,,,,//,给initialMulticaster 添加听众
  ,,,,,for  (ApplicationListener<?祝辞,listener :, application.getListeners ()), {
  ,,,,,,,,this.initialMulticaster.addApplicationListener(听众);
  ,,,,,}
  ,,}
  ,,//,实现类调用EventPublishingRunListener的方法
  ,,public  void 开始(),{
  ,,,,,,this.initialMulticaster.multicastEvent (
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

springboot2.0.6如何启动监听器