The event is actually the message body we want to send. This generally needs to be encapsulated according to our actual business. What type of data is needed is what to use. Type, add which fields are needed. Let's give a case.
package com.lsqingfeng.springboot.applicationEvent; import lombok.Getter; import lombok.Setter; import org.springframework.context.ApplicationEvent; /** * @className: MyApplicationEvent * @description: 事件封装 * @author: sh.Liu * @date: 2022-03-23 14:41 */ @Getter @Setter @ToString public class MyApplicationEvent extends ApplicationEvent { private Integer age; private String name; /** * 需要重写构造方法 * @param source * @param name * @param age */ public MyApplicationEvent(Object source, String name, Integer age) { super(source); this.name = name; this.age = age; } }
The listener is equivalent to the consumer of our MQ. When there is time to push it, the listener code can be executed. Here we set our event type through generics.
package com.lsqingfeng.springboot.applicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * @className: MyApplicationEventListener * @description:事件监听器 * @author: sh.Liu * @date: 2022-03-23 14:50 */ @Component public class MyApplicationEventListener implements ApplicationListener<MyApplicationEvent> { @Override public void onApplicationEvent(MyApplicationEvent event) { System.out.println("收到消息:" + event); } }
Push events require the use of ApplicationEventPublisher. This object is already in the container when the Spring container is loaded. So we can inject it directly or use ApplicationContext, because ApplicationContext itself inherits ApplicationEventPublisher. Let's verify it through a Controller.
package com.lsqingfeng.springboot.controller; import com.lsqingfeng.springboot.applicationEvent.MyApplicationEvent; import com.lsqingfeng.springboot.base.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @className: ApplicationEventController * @description: * @author: sh.Liu * @date: 2022-03-23 15:21 */ @RestController @RequestMapping("event") public class ApplicationEventController { @Autowired private ApplicationContext applicationContext; @RequestMapping("/push") public Result pushEvent(){ MyApplicationEvent myApplicationEvent = new MyApplicationEvent(this,"zhangsan", 10); applicationContext.publishEvent(myApplicationEvent); return Result.success(); } @RequestMapping("/push3") public Result pushEvent2(){ applicationContext.publishEvent("大家好"); return Result.success(); } }
We define two push methods. One pushes our MyApplicationEvent type, and another method pushes a string.
When we call the first method, the console can print out the data information we pushed.
When calling the push string, our listener will not execute because our interceptor has added the generic MyApplicationEvent, which means it will only listen to MyApplicationEvent. type of message. Other types of messages will not be listened to.
What will be the effect if we remove generics? Let’s try it.
Every push will send two messages (there may be some internal mechanism, I don’t care), but both are printed, indicating that if generics are not added, no matter who pushes , messages can be received here.
In addition to developing the listener by implementing the interface above, we can also implement it through annotations. The specific code is as follows.
package com.lsqingfeng.springboot.applicationEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; /** * @className: MyApplicationEventListener2 * @description: 注解实现监听器 * @author: sh.Liu * @date: 2022-03-23 15:56 */ @Component public class MyApplicationEventListener2 { @EventListener public void onEvent(MyApplicationEvent event){ System.out.println("收到消息2:" + event); } }
The @EventListener annotation is added here to represent that this is a listener. The method name is arbitrary, and the parameters in the method represent the event types to be monitored.
Call the push method again:
It is found that the data of both listeners will be printed. Everyone should pay attention to this feature.
The above is the detailed content of How to use ApplicationEvent and ApplicationListener in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!