Spring学习历程--- 一个监听器实例

37 篇文章 0 订阅
29 篇文章 2 订阅


MailSendEvent.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;

public class MailSendEvent extends ApplicationContextEvent {
	private String to;
	
	public MailSendEvent(ApplicationContext source, String to) {
		super(source);
		this.to = to;
	}
	public String getTo() {
		
		return this.to;
	}
}
MailSendListener.java

import org.springframework.context.ApplicationListener;

public class MailSendListener implements ApplicationListener<MailSendEvent>{

	public void onApplicationEvent(MailSendEvent event) {
			MailSendEvent mse = (MailSendEvent) event;
			System.out.println("MailSendListener:向" + mse.getTo() + "发送完一封邮件");
	}
}
MailSender.java

public class MailSender implements ApplicationContextAware {

	private ApplicationContext ctx ;

	public void setApplicationContext(ApplicationContext ctx)
			throws BeansException {
		this.ctx = ctx;

	}	
	public void sendMail(String to){
		System.out.println("MailSender:模拟发送邮件...");
		MailSendEvent mse = new MailSendEvent(this.ctx,to);
		ctx.publishEvent(mse);
	}
}
beans.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean class="com.baobaotao.event.MailSendListener"/>
     <bean id="mailSender" class="com.baobaotao.event.MailSender"/>
</beans>
ApplicationEventTest.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicatonEventTest {

	public static void main(String[] args) {
		String resourceFile = "com/baobaotao/event/beans.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(resourceFile);	
		MailSender mailSender = ctx.getBean(MailSender.class);
		mailSender.sendMail("test mail.");
	    System.out.println("done.");
	}
}
首先由sendMail方法产生一个MailSendEvent , 并把这个事件发送给所有事件监听器。

MailSendListener类继承了事件监听器接口ApplicationListener,处理了这个事件。

其中

public void onApplicationEvent(MailSendEvent event) {
			MailSendEvent mse = (MailSendEvent) event;
			System.out.println("MailSendListener:向" + mse.getTo() + "发送完一封邮件");
	}
这个方法就是处理事件的方法。


Spring只有两个事件监听器接口

1.ApplicaionListener , 只有一个方法

onApplicationEvent(E event) , 该方法接受ApplicationEvent 事件对象,在该方法中编写事件的响应处理逻辑。

2.SmartApplicationListener接口,有两个方法:

1)boolean supportsEventType(Class<? extends ApplicationEvent>eventType) : 指定监听器支持哪种类型的容器事件,即他只会对该类型的事件做出响应。

2)boolean supportsSourceType(Class<?> sourceType) : 该方法指定监听器仅对何种事件源对象做出响应。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值