网站建设基地企业网站优化价格
Spring Event学习
观察者模式是一种行为设计模式,它定义了对象之间的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并被自动更新。在这个模式中,改变状态的对象被称为主题,依赖的对象被称为观察者。
为什么要使用Spring Event
1、业务解耦:比如给注册成功的用户发送短信通知,正常情况下我们可能会在service中去写这个发送短信通知的代码,但是如果用户注册有多个场景,每个场景都去调一次发送短信通知的代码略显繁杂,而且注册业务和发送短信通知的业务耦合了;这时就可以使用Spring Event进行处理,在注册成功后发布一个用户注册成功的事件,然后在监听器中去统一发送短信通知;
2、底层模块给上层模块发送通知:如果项目本身依赖了自己开发的其它模块,那么业务模块如何拿到底层模块的信息呢?这时也可以使用Spring Event进行处理。
Spring Event使用
非注解
创建事件对象。这里要注意的是从Spring Framework4.2开始,ApplicationEventPublisher接口为 publishEvent(Object event)方法提供了新的重载,该方法接受任何对象作为事件。因此,Spring 事件不再需要扩展ApplicationEvent 类。
public class MyEvent extends ApplicationEvent {private String message;public MyEvent(Object source) {super(source);}public MyEvent(Object source, String message) {super(source);this.message = message;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
事件发布者,发布事件需要调用Spring提供的ApplicationEventPublisher的publishEvent方法。
@Component
public class MyEventPublisher {@Autowiredprivate ApplicationEventPublisher eventPublisher;public void publishMyEvent(String message) {System.out.println("message: " + message);MyEvent myEvent = new MyEvent(this, "test meaage");//发布事件,发布给在监听的任何人eventPublisher.publishEvent(myEvent);}
}
事件监听。事件监听可以通过实现ApplicationListener来实现。
@Component
public class MyEventListener implements ApplicationListener<MyEvent> {@Overridepublic void onApplicationEvent(MyEvent event) {System.out.println("recieve message: " + event.getMessage());}
}
在业务中发布事件,这里写了一个controller,当访问这个接口时会发送事件,事件监听者会收到事件通知。
@RestController
@RequestMapping("/test")
public class MyController {@Autowiredprivate MyEventPublisher eventPublisher;@RequestMapping("/test1")public String test1() {eventPublisher.publishMyEvent("test1");return "test1";}
}
注解
从 Spring4.2开始,事件侦听器可以不实现ApplicationListener接口,使用@EventListener注解即可。
使用注解的事件监听者示例如下:
@Component
public class AnnotationDrivenEventListener {@EventListenerpublic void handleContextStart(ContextStartedEvent cse) {System.out.println("Handling context started event.");}
}
默认情况下监听器是同步的,可以通过在方法上使用@Async注解来开启异步。
Spring 框架提供的事件
ContextRefreshed事件
在初始化或刷新ApplicationContext时,Spring 会引发 ContextRefreshedEvent。通常,只要上下文尚未关闭,刷新就会被多次触发。另外还可以调用ConfigurableApplicationContext接口的refresh方法来手动触发事件。
ContextStarted事件
通过在 ConfigurableApplicationContext 上调用start方法,我们触发此事件并启ApplicationContext。事实上,该方法通常用于在显式停止后重新启动 bean。我们还可以使用该方法来处理没有自动启动配置的组件。在这里,需要注意的是,对 start的调用始终是显式的,而不是refresh。
ContextStopped事件
当 ApplicationContext 停止时,通过调用 ConfigurableApplicationContext上的stop方法,将发布 ContextStoppedEvent。我们可以使用 start方法重新启动停止的事件。
ContextClosed事件
此事件在 ApplicationContext 关闭时使用 ConfigurableApplicationContext 中的 close方法发布。
实际上,在关闭上下文后,我们无法重新启动它。上下文在关闭它时会达到其生命周期的终点。
示例代码
监听指定的事件
@EventListener
public void handleContextRefreshEvent(ContextStartedEvent ctxStartEvt) {System.out.println("Context Start Event received.");
}
同时监听多个事件
@Component
public class MyContextStartListener {@EventListener(classes = {ContextStartedEvent.class, ContextClosedEvent.class,ContextStoppedEvent.class, ContextRefreshedEvent.class})public void handleContextStart(ApplicationContextEvent event) {System.out.println("Handling context started event.");}}
参考
- Spring高手之路7——事件机制与监听器的全面探索
- Spring Events
- Spring Application Context Events