Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
2017-12-24 11:56
495 查看
Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。
ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。
ApplicationEvent
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
ApplicationListener
是一个接口,里面只有一个onApplicationEvent方法。
所以自己的类在实现该接口的时候,要实装该方法。
如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Oberver设计模式。
下面给出例子:
首先创建一个ApplicationEvent实现类:
[html] view
plain copy
package com.spring.event;
import org.springframework.context.ApplicationEvent;
/**
* <p>Class:EmailListEvent</p>
* <p>Description:</p>
* @author Liam
* @Date [2012-9-7 上午8:42:07]
*/
public class EmailEvent extends ApplicationEvent {
/**
* <p>Description:</p>
*/
private static final long serialVersionUID = 1L;
public String address;
public String text;
public EmailEvent(Object source) {
super(source);
}
public EmailEvent(Object source, String address, String text) {
super(source);
this.address = address;
this.text = text;
}
public void print(){
System.out.println("hello spring event!");
}
}
给出监听器:
[html] view
plain copy
package com.spring.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
* <p>Class:EmailListener</p>
* <p>Description:</p>
* @author Liam
* @Date [2012-9-7 上午8:44:53]
*/
public class EmailListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof EmailEvent){
EmailEvent emailEvent = (EmailEvent)event;
emailEvent.print();
System.out.println("the source is:"+emailEvent.getSource());
System.out.println("the address is:"+emailEvent.address);
System.out.println("the email's context is:"+emailEvent.text);
}
}
}
applicationContext.xml文件配置:
<bean id="emailListener" class="com.spring.event.EmailListener"></bean>
测试类:
[html] view
plain copy
package com.spring.event;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* <p>Class:Test</p>
* <p>Description:</p>
* @author Liam
* @Date [2012-9-6 上午10:41:34]
*/
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//HelloBean hello = (HelloBean) context.getBean("helloBean");
//hello.setApplicationContext(context);
EmailEvent event = new EmailEvent("hello","boylmx@163.com","this is a email text!");
context.publishEvent(event);
//System.out.println();
}
}
[html] view
plain copy
测试结果:
[html] view
plain copy
[html] view
plain copy
hello spring event!
the source is:hello
the address is:boylmx@163.com
the email's context is:this is a email text!
相关文章推荐
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Springboot 事件监听
- Spring 中的事件监听的实现
- spring事件监听的功能
- Spring 注入的事件监听模式
- jQuery-mobile事件监听与用法详解
- JavaEE开发之Spring中的事件发送与监听以及使用Profile进行环境切换
- spring 事件监听
- Spring之事件监听
- jQuery绑定事件监听bind和移除事件监听unbind用法实例详解
- Spring之事件监听
- jQuery的on()方法的两种用法(委托给父元素进行事件监听代理)
- Spring中的IOC(四):IOC中其他接口的使用及Spring的事件处理机制(监听机制)
- springboot学习----事件监听
- spring @EventListener 事件与监听
- JavaScript使用addEventListener添加事件监听用法实例