Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand the java listener model

Shulou Source: shulou.com Published: 2022-06-01 23:11:09 09月10日 Update

This article introduces the knowledge of "how to understand the java listener mode". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The listener mode is used in many places in the project. For example, an interface uses ApplicationContextEvent (applicationContext.publishEvent) that comes with spring when doing asynchronous operations.

Here is a brief account of this important design pattern.

The listener pattern consists of an event Event corresponding to the listener Listener and an event publisher EventPublish. The process is that EventPublish publishes an event, which is captured by the listener, and then executes the corresponding method for the event.

1. Event definition

Public class EventDemo extends ApplicationEvent {private String message; public EventDemo (Object source, String message) {super (source); this.message = message;} public String getMessage () {return message;}}

@ EventListener @ Async

two。 Event listener

Componentpublic class EventDemoListern implements ApplicationListener {@ Override public void onApplicationEvent (EventDemo event) {System.out.println ("receiver" + event.getMessage ());}}

3. Event release

@ Componentpublic class EventDemoPublish {@ Autowired private ApplicationEventPublisher applicationEventPublisher; public void publish (String message) {EventDemo demo = new EventDemo (this, message); applicationEventPublisher.publishEvent (demo);}} Spring Event

ApplicationContextEvent (Context... Abstract class of)

ContextClosedEvent Lifecycle shutdown

ContextRefreshedEvent refresh completed

ContextStartedEvent Lifecycle start

ContextStoppedEvent Lifecycle stop

PayloadApplicationEvent

RequestHandledEvent

A subclass of ServletRequestHandledEvent RequestHandledEvent, the event pushed after the Spring MVC request is completed

This is the end of "how to understand java listener Mode". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Events monitoring patterns cycles life content more knowledge process practical important learned and then publishers dilemmas places subclasses actually that is the situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Microsoft Huawei OPPO Reno