spring 的事件发布以及监听器
2017-04-12 16:21
483 查看
观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式、模型-视图(Model/View)模式、源-监听器(Source/Listener)模式或从属者(Dependents)模式。
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。想要实现观察者模式,个人觉得需要三部分1:发布者,2.发布的事件,3.监听器。发布者见名之意,就是当程序发生变化,由发布者发布事件;发布的事件就是发布者要发布信息内容;监听器就是接受发布者发布的事件;然而强大的spring提供这样的几个接口,ApplicationEventPulisherAware,实现该接口的类相当于拥有了发布这个方法,即可理解为实现该接口的类是发布者;另外一个就是ApplicationEvent,是一个抽象类,继承该类重写了构造方法,实现该类必须重写构造方法,可将事件的内容封装在该对象的 protected
transient Object source中(如有兴趣了解代码);最后一个接口就是ApplicationListener<ApplicationEvent>,同理,该接口提供监听的方法,所以实现该接口的类都拥有该行为;下面贴上一个简单的dome;
发布者:
package com.yu.listener;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
@Component
public class TestPublisher implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
public void publishEvent(TestEvent testEvent){
applicationEventPublisher.publishEvent(testEvent);
}
};
事件对象:
package com.yu.listener;
import org.springframework.context.ApplicationEvent;
public class TestEvent extends ApplicationEvent {
/**
*
*/
private static final long serialVersionUID = 1L;
public TestEvent(Object source) {
super(source);
// TODO Auto-generated constructor stub
}
}
监听器对象:
package com.yu.listener;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class TestEventListener implements ApplicationListener<TestEvent>,Runnable{
private TestEvent event;
@Override
public void onApplicationEvent(TestEvent event) {
System.out.println(this);
// TODO Auto-generated method stub
TestEvent testEvent = (TestEvent)event;
this.event = testEvent;
Thread t1 = new Thread(this);
System.out.println("进入了监听器方法!");
t1.start();
}
@Override
public void run() {
System.out.println(this);
// TODO Auto-generated method stub
System.out.println("进入了线程的run方法!");
System.out.println(this.event);
}
};
在这里有个地方主要的是,创建线程要使用this对象。
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。想要实现观察者模式,个人觉得需要三部分1:发布者,2.发布的事件,3.监听器。发布者见名之意,就是当程序发生变化,由发布者发布事件;发布的事件就是发布者要发布信息内容;监听器就是接受发布者发布的事件;然而强大的spring提供这样的几个接口,ApplicationEventPulisherAware,实现该接口的类相当于拥有了发布这个方法,即可理解为实现该接口的类是发布者;另外一个就是ApplicationEvent,是一个抽象类,继承该类重写了构造方法,实现该类必须重写构造方法,可将事件的内容封装在该对象的 protected
transient Object source中(如有兴趣了解代码);最后一个接口就是ApplicationListener<ApplicationEvent>,同理,该接口提供监听的方法,所以实现该接口的类都拥有该行为;下面贴上一个简单的dome;
发布者:
package com.yu.listener;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
@Component
public class TestPublisher implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
public void publishEvent(TestEvent testEvent){
applicationEventPublisher.publishEvent(testEvent);
}
};
事件对象:
package com.yu.listener;
import org.springframework.context.ApplicationEvent;
public class TestEvent extends ApplicationEvent {
/**
*
*/
private static final long serialVersionUID = 1L;
public TestEvent(Object source) {
super(source);
// TODO Auto-generated constructor stub
}
}
监听器对象:
package com.yu.listener;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class TestEventListener implements ApplicationListener<TestEvent>,Runnable{
private TestEvent event;
@Override
public void onApplicationEvent(TestEvent event) {
System.out.println(this);
// TODO Auto-generated method stub
TestEvent testEvent = (TestEvent)event;
this.event = testEvent;
Thread t1 = new Thread(this);
System.out.println("进入了监听器方法!");
t1.start();
}
@Override
public void run() {
System.out.println(this);
// TODO Auto-generated method stub
System.out.println("进入了线程的run方法!");
System.out.println(this.event);
}
};
在这里有个地方主要的是,创建线程要使用this对象。
相关文章推荐
- springboot源码分析14-事件发布机制以及应用监听器
- spring自定义事件和事件监听器以及事件的发布-ApplicationEvent
- springboot源码分析14-事件发布机制以及应用监听器
- spring自定义事件和事件监听器以及事件的发布-ApplicationEvent
- Spring 4.2框架中注释驱动的事件监听器详解
- Spring 4.2框架中注释驱动的事件监听器详解
- spring 启动完成后事件监听器处理
- spring发布和接收定制的事件(spring事件传播)
- 在Spring中发布自定义事件
- 【Spring4揭秘 基础1】监听器和事件
- 一起学spring--spring事件机制--监听器
- spring发布和接收定制的事件(spring事件传播)
- Laravel 5.1 定义事件、事件监听器以及触发事件实例教程
- android事件监听器以及bitmap的运用
- spring 启动完成后事件监听器处理
- Spring进阶之路(2)-ApplicationContext容器以及事件机制
- activeMQ 点对点以及发布与订阅 - 以及spring的整合&集群方式
- Spring事件发布
- springmvc webservice的2种用法,xstream以及axis2 集成spring发布服务
- 比较C#的委托与C语言的函数指针,以及用流程图来理解C#中事件,发布与订阅的逻辑