In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of SpringBoot event release and monitoring, the article is very detailed, has a certain reference value, interested friends must read it!
Overview
ApplicationEvent and Listener are an implementation of event monitoring and subscription provided by Spring. The internal implementation principle is the observer design pattern, and the original purpose of the design is to decouple the business logic of the system and improve scalability and maintainability. The event publisher does not need to consider who is listening or what the specific implementation is, the publisher's job is to publish the event. Event listening is somewhat similar to message queuing.
The structure of event monitoring
It is mainly composed of three parts:
Publisher Publisher
Event Event
Listener Listener
The relationship between Publisher,Event and Listener
Event
Our custom event MyTestEvent inherits ApplicationEvent, and the constructor must be overloaded after inheritance. The constructor parameter can be specified arbitrarily, where the source parameter refers to the object in which the event occurs. Generally, when we publish the event, we use the this keyword instead of this kind of object, and the user parameter is our custom registered user object, which can be obtained in monitoring.
@ Getterpublic class MyTestEvent extends ApplicationEvent {private static final long serialVersionUID = 1L; private User user; public MyTestEvent (Object source, User user) {super (source); this.user = user;}}
Publisher
Event publishing is controlled by the ApplicationContext object. Before we publish the event, we need to inject the ApplicationContext object and call the publishEvent method to complete the event publication.
Although ApplicationEventPublisher applicationEventPublisher declares ApplicationEventPublisher, it actually injects applicationContext
@ RestController@RequestMapping ("/ test") public class TestController {@ Autowired ApplicationContext applicationContext; @ Autowired ApplicationEventPublisher applicationEventPublisher; @ GetMapping ("testEvent") public void test () {applicationEventPublisher.publishEvent (new MyTestEvent ("dzf-casfd-111", new User ("dzf-625096527-111"," xiaoming ", 19)); applicationEventPublisher.publishEvent (" dzf-49687489-111l ", new User (" dzf-625096527-111l "," xiaowang ", 20) }} listener
Interface-oriented programming to realize ApplicationListener interface
Componentpublic class MyTestListener implements ApplicationListener {@ Override public void onApplicationEvent (MyTestEvent myTestEvent) {System.out.println ("MyTestListener:" + myTestEvent.getUser ());}}
Use @ EventListener to annotate configuration
@ Componentpublic class MyTestListener2 {@ EventListener (MyTestEvent.class) public void onApplicationEvent (MyTestEvent myTestEvent) {System.out.println ("MyTestListener2:" + myTestEvent.getUser ());}} these are all the contents of the article "sample Analysis of SpringBoot event Publishing and Monitoring". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.