spring容器事件

spring的ApplicationContext能够发布事件并且允许注册相应的事件监听器,它拥有一套完善的事件发布和监听机制.


事件体系:事件,事件监听器,事件源,事件监听器注册表,事件广播器.其中,事件源是事件的产生者,事件监听器注册表就是保存事件监听器的地方,事件广播器是事件与事件监听器沟通的桥梁,负责将事件通知给事件监听器.


事件类:ApplicationEvent包含ApplicationContextEvent和RequestHandledEvent两个子类,容器事件ApplicationContextEvent有ContextClosedEvent,ContextRefreshedEvent,ContextStartedEvent,ContextStoppedEvent四个子类,Web应用相关事件RequestHandledEvent有ServletRequestHandledEvent,PortletRequestHandledEvent两个子类.
事件监听器接口:ApplicationListener<E extends ApplicationEvent>包含SmartApplicationListener子接口.
事件广播器:ApplicationEventMulticaster->AbstractApplicationEventMulticaster->SimpleApplicationEventMulticaster.


spring容器事件体系的具体实现:主要参看org.springframework.context.support.AbstractApplicationContext#refresh

...前略...
// Initialize event multicaster for this context.1.为此上下文初始化事件广播器
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
onRefresh();

// Check for listener beans and register them.2:检查并注册监听器
registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.3:发布相应事件
finishRefresh();
...后略...

1.检查是否有名为applicationEventMulticaster的bean,如果有,就会将此bean作为事件广播器,否则将SimpleApplicationEventMulticaster实例作为事件广播器
2.找出监听器并注册到事件监听器注册表
3.调用事件广播器发布相应事件


下面是个模拟实例:
1.定义事件.

package org.exam.event;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;
/**
 * Created by xin on 15/1/28.
 */
public class MailSenderEvent extends ApplicationContextEvent{
	private String to;
	public MailSenderEvent(ApplicationContext source,String to) {
		super(source);
		this.to=to;
	}
	public String getTo() {
		return to;
	}
}
2.定义事件监听.
package org.exam.event;
import org.springframework.context.ApplicationListener;
public class MailSenderListener implements ApplicationListener<MailSenderEvent> {
	@Override
	public void onApplicationEvent(MailSenderEvent event) {
		System.out.println(getClass()+":向"+event.getTo()+"发送完一封邮件!");
	}
}
3.使用org.springframework.context.ApplicationContext#publishEvent来发布事件.此类必须实现ApplicationContextAware接口,以便容器启动时注入ApplicationContext实例,这样此实例才有发布事件的能力.
package org.exam.event;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
/**
 * Created by xin on 15/1/28.
 */
public class MailSender implements ApplicationContextAware {
	private ApplicationContext applicationContext;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}
	public void sendMail(String to){
		System.out.println(getClass()+":模拟发送邮件:");
		ApplicationEvent applicationEvent=new MailSenderEvent(applicationContext,to);
		applicationContext.publishEvent(applicationEvent);
	}
}
4.测试.
package org.exam.config;
import org.exam.event.MailSender;
import org.exam.event.MailSenderListener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * Created by xin on 15/1/28.
 */
@Configuration
public class AppConfig {
	@Bean
	public ApplicationListener applicationListener(){
		return new MailSenderListener();
	}
	@Bean
	public MailSender mailSender(){
		return new MailSender();
	}
}

package org.exam.event;
import org.exam.config.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * Created by xin on 15/1/29.
 */
public class Main {
	public static void main(String[] args) {
		ApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);
		MailSender mailSender= (MailSender) context.getBean("mailSender");
		mailSender.sendMail("xiejx618@xx.io");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值