On spring boot event monitoring

Spring boot event listener: provides support for message communication between beans. When a bean has done something, it notifies another bean to know and deal with it accordingly. At this time, we need another bean to listen to the events of the current bean.

Implementation steps: four steps and four ways

The first way

1. Custom events generally inherit the applicationevent abstract class

2. Define event listeners, generally implementing the applicationlistener interface

3. 1) add listener to spring application: applicationlistener addListener(); Then publish the event 2) or place it in the spring container: @ contributor 3) or in application Configure context in the properties configuration file listener. Classes = listener full class name 4) or write a class, put it into the spring container, and write a method with parameters. The parameters are the parent class of the event or the event to be monitored, plus @ EventListener

4. Publish event

Step 1: customize the event and inherit the applicationevent abstract class

Step 2: customize the event listener and implement the applicationlistener interface

Step 3 and 4: add the listener to spring and publish the event

The second way: add the listener to the spring container in another way (just modify the blogapplication)

Add the listener to the spring container through the @ Component annotation

BlogApplication. class

The third way: in application Configure context in the properties configuration file listener. Classes = listener full class name

spring. properties

The fourth way: write a class myeventhandler to replace myapplicationlistener in the above, incorporate it into the spring container, and write a method with parameters. The parameters are the parent class of the event or the event to be monitored, plus @ EventListener. (the wider the range of parameters, the more events will be monitored.)

MyEventHandler. class

So far, the four event monitoring methods of springboot have been introduced ~, and the above is the whole content of this article. I hope it will be helpful for everyone's learning, and I hope you can support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>