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

How to understand the java listener model

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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!

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

Internet Technology

Wechat

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

12
Report