spring 的事件发布以及监听器
来源:互联网 发布:大数据分析可视化 编辑:程序博客网 时间:2024/06/06 00:15
观察者模式是对象的行为模式,又叫发布-订阅(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对象。
- spring 的事件发布以及监听器
- springboot源码分析14-事件发布机制以及应用监听器
- springboot源码分析14-事件发布机制以及应用监听器
- android事件监听器以及bitmap的运用
- Spring的事件发布机制
- window.onload以及事件监听器
- Spring 4.2框架中注释驱动的事件监听器详解
- Spring 4.2框架中注释驱动的事件监听器详解
- Spring 4.2框架中注释驱动的事件监听器详解
- spring的ApplicationContext,事件监听器与国际化消息
- Spring的事件和监听器-同步与异步
- Servlet的事件监听器
- Servlet的事件监听器
- spring发布和接收定制的事件(spring事件传播)
- spring发布和接收定制的事件(spring事件传播)
- spring发布和接收定制的事件(spring事件传播)
- spring发布和接收定制的事件(spring事件传播)
- spring发布和接收定制的事件(spring事件传播)
- json转string方法;string转json;
- 安装和使用VCLSkin美肤插件
- 使用Numpy读取CSV文件,并进行行列删除操作
- 标题超过自动截断
- 多线程数据同步之synchronized同步方法-多线程访问临界资源
- spring 的事件发布以及监听器
- 多线程_高并发
- Java Socket 发送邮件
- iis服务停止
- 树莓派3三款中文输入法安装图文教程
- 红宝书 第9章整理——客户端检测
- sql2005属性IsLocked不可用于登录sa解决方案
- 使用WinRadius服务器软件 搭建 radius 认证
- 利用tomcat7发布项目报内存溢出错误的解决方法之一