最近一个项目,我们应用了Spring的事件机制,它是基于观察者设计模式的思想,但是有点差异的是还需要实现ApplicationContextAware,因为我们要拿到applicationContext来触发我们的事件,然后由Spring回调,将对象传进来,这样我们就可以很方便的访问所有的Bean,或者做一些其他的事情,比如发布事件。
还需要实现一个ApplicationListener,这个就是我们的观察者了,表示当某个事件被触发了,我们该做点什么。
其次就需要去定义事件,例如流程结束事件,一个购物系统当订单流程已经结束了,我们需要通知发货员发货,这个时候我们的这个通知发货,就是一个观察者,我们需要实现通知,比如短信,邮件通知,来告知物流
定制自己的事件,需要继承ApplicationEvent。
下面这幅图,是我画的很简单的一个图,这就是核心的方法和核心的类。
然后我们看看源代码是怎么做的:
//这个其实很简单,调用转交给multicastEvent方法。
public void publishEvent(ApplicationEvent event) {
Assert.notNull(event, "Event must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Publishing event in " + getDisplayName() + ": " + event);
}
getApplicationEventMulticaster().multicastEvent(event);
if (this.parent != null) {
this.parent.publishEvent(event);
}
}
//当某个事件被触发,这里会轮训的通知整个Application的观察者,所以我们在每个观察中都要判断一下,该事件源和事件,是不是我们需要关心的。否则每个都要跑一遍
@SuppressWarnings("unchecked")
public void multicastEvent(final ApplicationEvent event) {
for (final ApplicationListener listener : getApplicationListeners(event)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
public void run() {
//这里回调我们的listener中的对应处理逻辑 listener.onApplicationEvent(event);
}
});
}
else {
listener.onApplicationEvent(event);
}
}
}
样例:
自定义的事件:
public class PersonFullEvent extends ApplicationEvent {
public PersonFullEvent(Object source) {
super(source);
}
}
public class MessageService implements ApplicationListener,
ApplicationContextAware {
private ApplicationContext context;
private List<Person> persons = new ArrayList<Person>(5);
public void addMessage(Person p) {
if (persons.size() > 5) {
context.publishEvent(new PersonFullEvent(p));
return;
}
persons.add(p);
}
public void onApplicationEvent(ApplicationEvent event) {
Object eventSource = event.getSource();
//这里一定要判断事件源
if (!(eventSource instanceof Person))
return;
//判断事件源之后,还要判断是否是这个事件,因为一个事件源可能有多个事件。但我们这里只是关心这个事件
if (event instanceof PersonFullEvent) {
System.out.println("test event successful");
}
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context = applicationContext;
}
测试代码:
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"bean.xml"});
MessageService ms = context.getBean(MessageService.class);
Person p=new Person();
for(int i=0;i<10;i++)
{
ms.addMessage(new Person());
}
Spring的另一个对外接口非常好用,ApplicationListener,这个接口是当Spring做某些事情之后会发出一个对应的事件,其实就是一个观察者模式.
比如:我们判断整个Spring容器启动完毕之后,我们需要做一些相应的动作,例如校验集群中是否存在同名服务器等,如果存在则退出。
public class ClusterCheck implements ApplicationListener, DisposableBean{
public void onApplicationEvent(ApplicationEvent event) {
//Spring容器启动完毕会发出ContextRefreshedEvent事件
if (event == null || !(event instanceof ContextRefreshedEvent))
return;
//然后这里做相应的自身的逻辑
}
}
- 大小: 17.3 KB
分享到:
相关推荐
主要介绍了使用Spring事件机制实现异步的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring框架作为Java开发的核心,其事件监听机制是理解和利用Spring的关键要素之一。这种机制基于观察者模式,由事件(ApplicationEvent)、事件监听器(ApplicationListener)和事件广播器...
NULL 博文链接:https://xls9577087.iteye.com/blog/2121752
Spring事件监听机制
微信支付-简单易用 & Spring ...使用 spring 事件机制封装了微信支付通知,以简化使用方式。wiremock 是一个 web mock server,配置中可配置微信 API 的 basePath, 所以你可以使用 wiremock 构建一个完全的仿真环境。
Spring 事件机制代码,有效解决了软件之间的耦合 学习项目代码
对spring代理机制的实现和原理的详细讲解
spring传播机制个人学习
如何通过spring自己的缓存机制缓存方法和数据,使系统性能更高!
希望大家看完这篇文章后能对Spring有更深入的了解,希望这篇文章对你有所帮助
讲述spring事务机制,aopafter,aopbefore,aopmethodintercept,4个例子
Spring ApplicationContext 事件机制
全面的Spring 核心机制 依赖注入 的描述,运用,实例开发运用,
Spring AOP的实现机制中文版,动态代理及原理,自定义类加载器
主要介绍了EventBus与Spring Event区别,需要的朋友可以参考下
SpringIOC和AOP实现机制模拟,来自与网络。
【免费】深入描述spring的事务处理机制,很不错的资源。
Spring Security以前叫做acegi,是后来才成为Spring的一个子项目,也是目前最为流行的一个安全权限管理框架,它与Spring紧密结合在一起。 Spring Security关注的重点是在企业应用安全层为您提供服务,你将发现业务...
上文理解到对spring事物,事物的隔离机制,这片具体说下事物的传播机制和隔离机制
spring安全机制demo 暂时还不能用 但是可以看出思路 入门