current position:Home>Three minute quick start of spring event mechanism # yyds dry goods inventory #

Three minute quick start of spring event mechanism # yyds dry goods inventory #

2022-01-27 01:06:59 Java4ye

How are you guys ~ Let's share this with you today Spring Event mechanism

Content overview




This familiar Observer mode My little friend should see it at a glance ~

It's actually a simple version Release - A subscription model

There are three core classes

  1. event ApplicationEvent
  2. Event publisher ApplicationEventPublisher
  3. Event listener ApplicationListener


The event publisher publishes events , The listener listens to the corresponding event and responds accordingly ~

Why use

Because the demand is uncertain


Leader : Want to send an email notification after the user's operation A

I : well

A few days later ……

Leader : Forget it , Just remind me in the station first , We'll consider sending text messages later , See which works better

I :…… well

I didn't use this event mechanism at first , I wrote it together without much thought

There's something wrong in the back , Just remember to use this


  1. decoupling You can send business code and e-mail , Station reminder , SMS, etc
  2. Asynchronous notification Don't wait for these events to complete
  3. Improve scalability It's much more convenient to expand other operations later


Small systems can also use , It is reliable to directly use message oriented middleware to solve the larger problems

Project practice



Send SMS and e-mail according to the feedback events of small partners

Main source code

One . Small partner feedback events ReaderFeedbackEvent


Characteristics and functions

Inherit ApplicationEvent , Save feedback DTO

Two . Event publisher ReaderFeedbackEventPublisher


Characteristics and functions

Realization ApplicationEventPublisherAware This interface , obtain ApplicationEventPublisher The event publisher publishes Events

3、 ... and . monitor ReaderFeedbackEvent Event and send mail


Characteristics and functions

Realization ApplicationListener This interface , Get feedback from the event , And send mail

Four . Send SMS notification only when the feedback is a little like


Characteristics and functions

The second way to write a listener —— annotation , It's more convenient , Listen here and judge whether you like it or not , Then send text messages


This is directly over here IDEA Intermediate testing ,


The effect is as follows

like = false


like = true


Received text messages

<img src="" alt="image-20210829124817280" style="zoom:67%;" />


You can see that the function module is very simple , Listen for feedback events , Then send text messages and emails ~


This part of the code is uploaded to my GitHub The warehouse ( The address is at the end of the article ), If you are interested, you can have a look , If you like, give it to star ah , thank !

Spring Source code interpretation

asynchronous / Sync

Here we make a breakpoint , You can easily come here


This method is Event multicast SimpleApplicationEventMulticaster in , You can see that when this is configured Thread pool when , It's going asynchronous Inform the route

Then how to call the listener ?

You can see that it's simple 3 Step , The last is to go back to our onApplicationEvent Method

invokeListener ——> doInvokeListener ——> onApplicationEvent



Active learning and flexible application , Go and try it !

Project address

Like it , Please order a little star


The mind map of this issue is as follows


ps: I've been busy recently , The speed will be slower ~

I am a 4ye We should …… See you soon !!

If you love, you can pay attention to the official account Java4ye Under the support 4ye ah

copyright notice
author[Java4ye],Please bring the original link to reprint, thank you.

Random recommended