文档章节

《Spring Recipes》第二章笔记:event-based communication

LiJIaming
 LiJIaming
发布于 2012/05/19 10:02
字数 596
阅读 137
收藏 0

《Spring Recipes》第二章笔记:event-based communication


问题

在bean之间添加事件驱动的通信。

解决方案

1、事件类需要继承ApplicationEvent。实现构造函数,在事件类中添加用于传递信息的属性。
2、事件发布者需要实现ApplicationEventPublisherAware接口,实现setApplicationEventPublisher方法,让容器注入ApplicationEventPublisher接口,并调用ApplicationEventPublisher接口的publish方法发布事件。
3、事件监听者需要实现ApplicationListener接口,实现onApplicationEvent方法接受发布的事件。
4、让容器管理事件发布者和事件监听者。

例:
事件类MyEvent:
public class MyEvent extends ApplicationEvent {

	private int i;
	
	public MyEvent(Object source,int i) {
		super(source);
		this.i = i;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}
	
	

}

事件发布者MyEventPublisher:
public class MyEventPublisher implements ApplicationEventPublisherAware {

	private ApplicationEventPublisher publisher;
	
	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
		this.publisher = publisher;		
	}
	
	public void publish(ApplicationEvent event) {
		this.publisher.publishEvent(event);
	}
	

}

事件监听者MyEventListener:
public class MyEventListener implements ApplicationListener<MyEvent> {

	private MyEvent event;
	
	@Override
	public void onApplicationEvent(MyEvent event) {
		this.event = event;
	}
	
	public void print() {
		System.out.println(this.event.getI());
	}

}

配置文件:
<bean name="publisher" class="com.ljm.springrecipses.event.MyEventPublisher" />
	
	<bean name="listener" class="com.ljm.springrecipses.event.MyEventListener" />


调用程序:
public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("com/ljm/springrecipses/event/beans.xml");
		MyEventPublisher pub = ctx.getBean(MyEventPublisher.class);
		//构造事件
                MyEvent e = new MyEvent(new MyEventCommunicationTest(), 10);
                //发布事件
		pub.publish(e);
	 	MyEventListener lis = ctx.getBean(MyEventListener.class);
                //打印事件结果
	 	lis.print();
	}


Spring内部事件


Spring有很多自带的内部事件,用户可以通过实现ApplicationListener进行监听:

Table 4.7. Built-in Events

Event Explanation
ContextRefreshedEvent Published when the ApplicationContext is initialized or refreshed, for example, using the refresh() method on the ConfigurableApplicationContext interface. "Initialized" here means that all beans are loaded, post-processor beans are detected and activated, singletons are pre-instantiated, and the ApplicationContext object is ready for use. As long as the context has not been closed, a refresh can be triggered multiple times, provided that the chosen ApplicationContext actually supports such "hot" refreshes. For example, XmlWebApplicationContext supports hot refreshes, but GenericApplicationContext does not.
ContextStartedEvent Published when the ApplicationContext is started, using the start() method on the ConfigurableApplicationContext interface. "Started" here means that all Lifecycle beans receive an explicit start signal. Typically this signal is used to restart beans after an explicit stop, but it may also be used to start components that have not been configured for autostart , for example, components that have not already started on initialization.
ContextStoppedEvent Published when the ApplicationContext is stopped, using the stop() method on the ConfigurableApplicationContext interface. "Stopped" here means that all Lifecycle beans receive an explicit stop signal. A stopped context may be restarted through a start() call.
ContextClosedEvent Published when the ApplicationContext is closed, using the close() method on the ConfigurableApplicationContext interface. "Closed" here means that all singleton beans are destroyed. A closed context reaches its end of life; it cannot be refreshed or restarted.
RequestHandledEvent A web-specific event telling all beans that an HTTP request has been serviced. This event is published after the request is complete. This event is only applicable to web applications using Spring's DispatcherServlet.



© 著作权归作者所有

共有 人打赏支持
LiJIaming
粉丝 22
博文 58
码字总数 25865
作品 0
深圳
程序员
私信 提问
《Spring Recipes》第二章笔记:Declaring Beans from Obje...

《Spring Recipes》第二章笔记:Declaring Beans from Object Properties 问题 在配置文件中将一个对象的属性声明为bean。 解决方案 使用Spring自带的PropertyPathFactoryBean工厂bean或者使...

LiJIaming
2012/05/17
0
0
《Spring Recipes》第二章笔记:Setting Bean Scopes

《Spring Recipes》第二章笔记:Setting Bean Scopes 问题 Spring容器默认创建的bean都是单例。 解决方案 Spring容器的<bean>元素的scope属性指定了bean的scope。 Table 4.3. Bean scopes 例...

LiJIaming
2012/05/17
0
0
《Spring Recipes》第二章笔记:Declaring Beans from Stat...

《Spring Recipes》第二章笔记:Declaring Beans from Static Fields 问题 在配置文件中将类的静态属性声明为bean。 解决方案 在bean元素中使用Spring自带的FieldRetrievingFactoryBean实例化...

LiJIaming
2012/05/16
0
0
《Spring Recipes》第二章笔记:Loading External Resources

《Spring Recipes》第二章笔记:Loading External Resources 问题 程序需要从不同的位置(文件系统,classpath,URL)读取不同类型的资源(如文本文件,XML文件,properties文件或者图片)。程...

LiJIaming
2012/05/18
0
0
《Spring Recipes》第二章笔记:Customizing Bean Initiali...

《Spring Recipes》第二章笔记:Customizing Bean Initialization and Destruction 问题 用户想要指定Spring容器在创建完一个bean后,立刻调用一个PostConstruct方法;或者在销毁一个bean之前...

LiJIaming
2012/05/17
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周二乱弹 —— 哥们之间报恩的想法被上帝实现了

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 小小编辑:推荐歌曲《消愁》 《消愁》- 毛不易 手机党少年们想听歌,请使劲儿戳(这里) @过遥 :周一的早上就应该用来补觉,太困了 周末不想...

小小编辑
12分钟前
10
3
MariaDB 服务器在 MySQL Workbench 备份数据的时候出错如何解决

服务器是运行在 MariaDB 10.2 上面的,在使用 MySQL Workbench 出现错误: mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"'......

honeymose
今天
3
0
apache顶级项目(二) - B~C

apache顶级项目(二) - B~C https://www.apache.org/ Bahir Apache Bahir provides extensions to multiple distributed analytic platforms, extending their reach with a diversity of s......

晨猫
今天
7
0
day152-2018-11-19-英语流利阅读

“超级食物”竟然是营销噱头? Daniel 2018-11-19 1.今日导读 近几年来,超级食物 superfoods 开始逐渐走红。不难发现,越来越多的轻食餐厅也在不断推出以超级食物为主打食材的健康料理,像是...

飞鱼说编程
今天
18
1
SpringBoot源码:启动过程分析(二)

接着上篇继续分析 SpringBoot 的启动过程。 SpringBoot的版本为:2.1.0 release,最新版本。 一.时序图 一样的,我们先把时序图贴上来,方便理解: 二.源码分析 回顾一下,前面我们分析到了下...

Jacktanger
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部