This article brings you a detailed analysis of the Spring event mechanism source code. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Foreword: Since there were problems using the Spring event mechanism before, I went to understand the source code of this piece. The Spring event mechanism is actually event publishing/subscription (note that subscription in Spring refers to listening).
PS: Spring version is 5.1.5.RELEASE
Source code analysis
Initialization
The key to initialization is the registration of core components
1. Initialization and registration of ApplicationEventPublisher. The key method is the method prepareBeanFactory() of AbstractApplicationContext
2. Initialization and registration of ApplicationEventMulticaster. The key method is the initApplicationEventMulticaster() method of AbstractApplicationContext
3. The initialization and registration of ApplicationListener, the key method is the registerListeners() method of AbstractApplicationContext
I won’t go into details here, those who are interested can track the key methods by themselves
Event publishing/subscription
The key method for event publishing/subscription is publishEvent of AbstractApplicationContext. The source code is as follows:
protected void publishEvent(Object event, ResolvableType eventType) { // 避免空指针 Assert.notNull(event, "Event must not be null"); if (logger.isTraceEnabled()) { logger.trace("Publishing event in " + getDisplayName() + ": " + event); } // 处理event对象,将其转换为ApplicationEvent ApplicationEvent applicationEvent; if (event instanceof ApplicationEvent) { applicationEvent = (ApplicationEvent) event; } else { applicationEvent = new PayloadApplicationEvent<object>(this, event); if (eventType == null) { eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType(); } } // 是否延迟多播,即将事件发布到所有监听器中 if (this.earlyApplicationEvents != null) { this.earlyApplicationEvents.add(applicationEvent); } else { //此处为事件监听处理器的调用关键 getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType); } // 是否将事件发布到父容器中 if (this.parent != null) { if (this.parent instanceof AbstractApplicationContext) { ((AbstractApplicationContext) this.parent).publishEvent(event, eventType); } else { this.parent.publishEvent(event); } } }</object>
Through code tracking, it was found that Spring uses the default implementation of ApplicationEventMulticaster, SimpleApplicationEventMulticaster, to trigger event monitoring. The key method It is the multicastEvent() method, and the source code is as follows:
@Override 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) {//线程池不为null,则异步调用监听器 executor.execute(new Runnable() { @Override public void run() { invokeListener(listener, event); } }); } else {// 同步调用监听器 invokeListener(listener, event); } } }
This article has ended here. For more other exciting content, you can pay attention to the Java Video Tutorial column of the PHP Chinese website!
The above is the detailed content of Detailed analysis of Spring event mechanism source code. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version