Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to monitor the occurrence of ApplicationEvent events in spring

Shulou Source: shulou.com Published: 2022-06-02 10:27:27 09月12日 Update

This article will explain in detail how to monitor ApplicationEvent events in spring. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Principle: ApplicationContextAware interface provides publishEvent method, realizes the propagation mechanism of Observe (Observer) design pattern, and realizes the propagation of bean. Through ApplicationContextAware, we can spread all the ApplicationEvent in the system to all the ApplicationListener in the system. 1. Directly go to code 2, define your own listener (responsible for handling your own listening events) 3, define a bean trigger listening event 4, test package com.test.eventListener;import org.springframework.context.ApplicationEvent / * [@ author] (https://my.oschina.net/arthor) admin * [@ date] (https://my.oschina.net/u/2504391) 2018-5-17 17:37 * create a new StudentAddEvent class, and implement your own constructor in the abstract class org.springframework.context.ApplicationEvent * StudentAddEvent class * add student monitoring events * / public class StudentAddEvent extends ApplicationEvent {private static final long serialVersionUID = 20L) / * * Student name * / private String name; / * * [@ param] (https://my.oschina.net/u/2303379) source * / public StudentAddEvent (Object source, String name) {super (source); this.name = name;} public String getName () {return name } public void setName (String name) {this.name = name;}} package com.test.eventListener;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component / * [@ author] (https://my.oschina.net/arthor) admin * create a new StudentAddListener class to implement the onApplicationEvent method in the interface org.springframework.context.ApplicationListener. * only handle ApplicationEvent events of type StudentAddEvent in this method * define StudentAddListener listeners * / [@ Component] (https://my.oschina.net/u/3907912)public class StudentAddListener implements ApplicationListener {public void onApplicationEvent (ApplicationEvent event) {/ / 1. Determine whether the event if (! (event instanceof StudentAddEvent)) {return;} / / 2 is added to the student object. It is the object to add student events for logical processing, such as StudentAddEvent studentAddEvent = (StudentAddEvent) event; System.out.println ("added student:" + studentAddEvent.getName ());}} package com.test.eventListener;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component. / * @ author admin * define StudentAddBean trigger StudentAddEvent event * create a new StudentAddBean class to implement the setApplicationContext method in the interface org.springframework.context.ApplicationContextAware, * inject the Spring context object when constructing bean to trigger the StudentAddEvent event through the publishEvent method of the Spring context object * / @ Componentpublic class StudentAddBean implements ApplicationContextAware {/ * define the Spring context object * / private ApplicationContext applicationContext = null / * * (non-Javadoc) * * @ see * org.springframework.context.ApplicationContextAware#setApplicationContext * (org.springframework.context.ApplicationContext) * / public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext } / * * add a student * * @ param studentName * / public void addStudent (String studentName) {/ / 1. Construct an event that adds students StudentAddEvent aStudentEvent = new StudentAddEvent (applicationContext, studentName); / / 2. Trigger the addition of a student event applicationContext.publishEvent (aStudentEvent);}} package com.test.eventListener;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @ author admin * ApplicationContext automatically detects all bean objects that implement ApplicationListener at run time and receives them as event recipients. * when the publishEvent method of ApplicationContext is triggered, every bean that implements the ApplicationListener interface will receive an ApplicationEvent object. * each ApplicationListener can only receive and process events of interest according to the event type. For example, the above StudentAddListener only receives StudentAddEvent events. * / public class EventListenerTest {public static void main (String [] args) {String [] xmlConfig = new String [] {"spring/spring.xml"}; / / use ApplicationContext to initialize the system ApplicationContext context = new ClassPathXmlApplicationContext (xmlConfig); StudentAddBean studentBean = (StudentAddBean) context.getBean ("studentAddBean"); studentBean.addStudent ("Zhang San"); studentBean.addStudent ("Li Si") }} about how to monitor ApplicationEvent events in spring is now shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Events students objects monitoring methods interfaces processing up and down context system propagation content articles more listeners knowledge articles types good code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux MariaDB MySQL macOS