spring boot 监听容器启动代码实例

 更新时间:2019年10月16日 16:55:23   作者:匠心编程  
这篇文章主要介绍了spring boot 监听容器启动代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在使用Spring框架开发时, 有时我们需要在spring容器初始化完成后做一些操作, 那么我们可以通过自定义ApplicationListener 来实现.

自定义监听器

@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

  @Override
  public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

    // 获取spring 上下文
    ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();

    // do your work...
}

源码分析

springboot 启动应用, 执行 SpringApplication.run(String… args) 方法

run方法中执行完初始化上下文方法后, 会执行this.refreshContext方法, 刷新

经过一堆方法跳转, 执行 AbstractApplicationContextl类的publishEvent(Object event, @Nullable ResolvableType eventType) 方法

然后执行 SimpleApplicationEventMulticaster.multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType)

然后依次执行所有监听器的onApplicationEvent()方法

// 遍历所有ApplicationListeners, 依次执行ApplicationListener 的onApplicationEvent()方法
public void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) {
  ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
  Iterator var4 = this.getApplicationListeners(event, type).iterator();

  while(var4.hasNext()) {
    ApplicationListener<?> listener = (ApplicationListener)var4.next();
    Executor executor = this.getTaskExecutor();
    if (executor != null) {
      executor.execute(() -> {
        this.invokeListener(listener, event);
      });
    } else {
      this.invokeListener(listener, event);
    }
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java Collection 之List学习介绍

    java Collection 之List学习介绍

    本篇文章小编为大家介绍,java Collection 之List学习介绍。需要的朋友参考下
    2013-04-04
  • Java设计模式之桥接模式的示例详解

    Java设计模式之桥接模式的示例详解

    桥梁模式是对象的结构模式。又称为柄体(Handle and Body)模式或接口(Interface)模式。本文将通过示例来详细讲解一下这个模式,感兴趣的可以学习一下
    2022-02-02
  • SpringBoot如何使用ApplicationContext获取bean对象

    SpringBoot如何使用ApplicationContext获取bean对象

    这篇文章主要介绍了SpringBoot 如何使用ApplicationContext获取bean对象,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java代码生成器的制作流程详解

    Java代码生成器的制作流程详解

    这篇文章主要介绍了Java代码生成器的制作流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 在Spring Boot中启用HTTPS的方法

    在Spring Boot中启用HTTPS的方法

    本文介绍了在Spring Boot项目中启用HTTPS的步骤,从生成SSL证书开始,到配置Spring Boot。HTTPS是保护Web应用程序安全的基石之一,而Spring Boot则提供了相对简易的途径来配置它,感兴趣的朋友跟随小编一起看看吧
    2024-02-02
  • 一起聊聊Java中的自定义异常

    一起聊聊Java中的自定义异常

    在学习Java的过程中,想必大家都一定学习过异常这个篇章,异常的基本特性和使用这里就不再多讲了。本文就来和大家讲讲如何自定义异常
    2022-08-08
  • SpringCloud Open feign 使用okhttp 优化详解

    SpringCloud Open feign 使用okhttp 优化详解

    这篇文章主要介绍了SpringCloud Open feign 使用okhttp 优化详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • 如何修改json字符串中某个key对应的value值

    如何修改json字符串中某个key对应的value值

    这篇文章主要介绍了如何修改json字符串中某个key对应的value值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • Spring Boot超详细讲解请求处理流程机制

    Spring Boot超详细讲解请求处理流程机制

    SpringBoot是一种整合Spring技术栈的方式(或者说是框架),同时也是简化Spring的一种快速开发的脚手架,本篇让我们一起分析请求处理流程机制
    2022-07-07
  • Spring Cloud Feign组件实例解析

    Spring Cloud Feign组件实例解析

    这篇文章主要介绍了Spring Cloud Feign组件实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11

最新评论