Spring 事件机制
Spring
事件机制的主要成员:
- 事件(继承自ApplicationEvent)
- 事件监听器(两种方式,一种继承自接口ApplicationListener, 另一种是使用@Listener注解)
- 事件源(发布事件)【两种方式,一是使用ApplicationEventPublisher,二是使用ApplicatonContext(其后台)】
Demo:
用户定义,自定义事件定义
@Data public class User { private Long id; private String name; public User(Long id, String name) { this.id = id; this.name = name; } } //user event public class UserAddedEvent extends ApplicationEvent { public UserAddedEvent(Object source) { super(source); } }
//两种发布消息方式
@Service("eventUserService")
public class UserService implements ApplicationContextAware, ApplicationEventPublisherAware {
private ApplicationContext applicationContext;
private ApplicationEventPublisher applicationEventPublisher;
//使用 ApplicationContext 发布消息
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
//使用 ApplicationEventPublisher 发布消息
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
public String addUser(User user) {
// 保存用户
user.setId(1L);
user.setName("name-1");
// 发生事件(发邮件、发短信、、、)
// applicationContext.publishEvent(new UserAddedEvent(user));
// 两种发生方式一致
applicationEventPublisher.publishEvent(new UserAddedEvent(user));
return "ok";
}
}
//两种监视器
//1. 实现 接口ApplicationListener
@Component public class NotifySelfListenr implements ApplicationListener<UserAddedEvent> { @Override public void onApplicationEvent(UserAddedEvent userAddedEvent) { //根据类型匹配, // 只监视UserAddedEvent 事件 System.out.println("self event " + userAddedEvent.getClass().toString()); } } ///全局监视 @Component public class GlobalNotifyListenr implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { //监听所有事件,全局事件 System.out.println("GlobalNotifyListenr " + event.getClass().toString()); } }
//2.实现用注解 @EventListener
@Component public class UserListener { @EventListener public void getUserEvent(UserAddedEvent userEvent) { System.out.println("getUserEvent-接受到事件:" + userEvent); } @EventListener public void getUserEvent2(UserAddedEvent userEvent) { System.out.println("getUserEvent2-接受到事件:" + userEvent); } }
//运行代码
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { ConfigurableApplicationContext context= SpringApplication.run(DemoApplication.class, args); UserService userService = (UserService) context.getBean("eventUserService"); userService.addUser(new User((long) 11,"abc")); } }
//结果:
GlobalNotifyListenr class org.springframework.context.event.ContextRefreshedEvent
GlobalNotifyListenr class org.springframework.boot.context.event.ApplicationStartedEvent
GlobalNotifyListenr class org.springframework.boot.context.event.ApplicationReadyEvent
getUserEvent-接受到事件:com.feilong.com.demo.UserAddedEvent[source=User(id=1, name=name-1)]
getUserEvent2-接受到事件:com.feilong.com.demo.UserAddedEvent[source=User(id=1, name=name-1)]
GlobalNotifyListenr class com.feilong.com.demo.UserAddedEvent
self event class com.feilong.com.demo.UserAddedEvent
GlobalNotifyListenr class org.springframework.context.event.ContextClosedEvent
@EventListener 原理
@EventListener注解的自定义名称的方法,会在EventListenerMethodProcessor中的afterSingletonsInstantiated()方法中遍历所有 ApplicationContext容器的单利bean。将所有添加了@EventListener的方法注入到ApplicationContext的applicationListeners和初始化的SimpleApplicationEventMulticaster的defaultRetriever.applicationListeners中,在发送事件时候获取监听列表时用。
————————————————
参考博客:https://blog.csdn.net/it_lihongmin/article/details/102940643 等

浙公网安备 33010602011771号