Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Revealing the secrets of Spring architecture-event monitoring mechanism

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. Overview of event monitoring mechanism

Second, the structure of event monitoring mechanism

Third, the framework of Spring snooping mechanism

Spring's Application has the ability to publish events and register event listeners, and has a complete set of event publishing and listening mechanisms. In Java, through java.util. EventObject to describe the event, through java.util. EventListener to describe event listeners, in many frameworks and components, the establishment of a set of event mechanism is usually based on these two interfaces to expand.

In an event system, there are several important concepts.

1. Event source: the generator of the event object. Any EventObject has a source.

2. Event listener registry: when an event is received by the event framework or component, all relevant event listeners need to be notified for processing. At this time, there needs to be a place to store the listeners, that is, the event listener registry.

3. Event broadcaster: the event broadcaster plays the role of an intermediary in the whole event mechanism. when an event publisher publishes an event, it needs to notify all relevant listeners to handle the event through the broadcaster.

The following figure is the download of the structure diagram of the event mechanism

Spring event publishing mechanism

In Spring, the event mechanism is probably the same structure, and the specific implementation uses the observer pattern. Let's take a look at Spring's event mechanism design class diagram download.

1. ApplicationEventPublisher is the event publishing interface of Spring, and the event source publishes events through the pulishEvent method of this interface.

2. ApplicationEventMulticaster is the event broadcaster in the Spring event mechanism. It provides a SimpleApplicationEventMulticaster implementation by default. If the user does not have a custom broadcaster, the default one is used. It obtains the event listener from the event registry through the getApplicationListeners method of the parent class AbstractApplicationEventMulticaster, and executes the specific logic of the listener through the invokeListener method.

3. ApplicationListener is the event listener interface of Spring, and all listeners implement this interface. Several typical subclasses are listed in this figure. RestartApplicationListnener is downloaded in the startup framework of SpringBoot.

4. In Spring, ApplicationContext itself usually plays the role of listener registry, aggregates event broadcaster ApplicationEventMulticaster and event listener ApplicationListnener in its subclass AbstractApplicationContext, and provides addApplicationListnener methods for registering listeners.

Through the figure above, we can clearly know that when an event source produces an event, it publishes the event through the event publisher ApplicationEventPublisher, and then the event broadcaster ApplicationEventMulticaster will go to the event registry ApplicationContext to find the event listener ApplicationListnener, and execute the onApplicationEvent method of the listener one by one, thus completing the logic of the event listener.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report