---
title: "Spring 的 ApplicationEvent and ApplicationListener"
author: "tjukk"
date: "2015-02-27T15:55:48Z"
tags: ["Spring"]
categories: ["企业架构"]
slug: "spring-applicationevent-and-applicationlistener"
draft: false
lastmod: "2015-02-27T15:55:48Z"
id: 2187200
url: "https://tjukk.iteye.com/blog/2187200"
---

**什么是ApplicationContext?**   
它是Spring的核心，Context我们通常解释为上下文环境，但是理解成容器会更好些。   
ApplicationContext则是应用的容器。

Spring把Bean（object）放在容器中，需要用就通过get方法取出来。

**ApplicationEvent**

是个抽象类，里面只有一个构造函数和一个长整型的timestamp。

**ApplicationListener**

是一个接口，里面只有一个onApplicationEvent方法。

所以自己的类在实现该接口的时候，要实装该方法。

**如果在上下文中部署一个实现了ApplicationListener接口的bean,**

**那么每当在一个ApplicationEvent发布到 ApplicationContext时，**  
**这个bean得到通知。其实这就是标准的Observer设计模式。**

**首先创建一个Event事件类：**

 

**\[java]** [view plain](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "view plain")[copy](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "copy")

 

01. 1. public class EmailListEvent extends ApplicationEvent {
02. 2\.
03. 3.     private static final long serialVersionUID = 1L;
04. 4.     public String address;
05. 5.     public String text;
06. 6\.
07. 7.     public EmailListEvent(Object source) {
08. 8.         super(source);
09. 9.     }
10. 10\.
11. 11.     public EmailListEvent(Object source, String address, String text) {
12. 12.         super(source);
13. 13.         this.address = address;
14. 14.         this.text = text;
15. 15.     }
16. 16\.
17. 17.     public void print() {
18. 18.         System.out.println("Hello,Spring Event!!!");
19. 19.     }
20. 20. }

 

**其次创建一个ApplicationListener类：**

 

**\[java]** [view plain](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "view plain")[copy](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "copy")

 

01. 1. public class EmailNotifier implements ApplicationListener {
02. 2\.
03. 3.     public void onApplicationEvent(ApplicationEvent evt) {
04. 4.         if (evt instanceof EmailListEvent) {
05. 5.             EmailListEvent emailEvent = (EmailListEvent) evt;
06. 6.             emailEvent.print();
07. 7.             System.out.println("the source is:" + emailEvent.getSource());
08. 8.             System.out.println("the address is:" + emailEvent.address);
09. 9.             System.out.println("the mail's context is :" + emailEvent.text);
10. 10.         }
11. 11\.
12. 12.     }
13. 13. }

 

**接着将Listener注册到Spring的xml文件中：**

 

**\[html]** [view plain](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "view plain")[copy](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "copy")

 

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:aop="http://www.springframework.org/schema/aop"
05. xmlns:tx="http://www.springframework.org/schema/tx"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
08. http://www.springframework.org/schema/aop
09. http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
10. http://www.springframework.org/schema/tx
11. http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&gt;
12. &lt;bean id="emailListListener" class="spring.event.EmailNotifier"&gt;&lt;/bean&gt;
13. &lt;/beans&gt;

 

**最后创建Demo类：**

 

**\[java]** [view plain](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "view plain")[copy](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "copy")

 

01. 1. public class ListenerEventDemo {
02. 2\.
03. 3.     /\**
04. 4.      * @param args
05. 5.      \*/
06. 6.     public static void main(String\[] args) {
07. 7.         ApplicationContext context = new ClassPathXmlApplicationContext(
08. 8.                 "SpringEvent.xml");
09. 9.         EmailListEvent emailListEvent = new EmailListEvent("hello",
10. 10.                 "helloSpring@sina.com", "this is a test eamil content");
11. 11.         //在ApplicationContext中发布一个 ApplicationEvent
12. 12.         context.publishEvent(emailListEvent);
13. 13.     }
14. 14\.
15. 15. }

 

**测试结果：**

**\[plain]** [view plain](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "view plain")[copy](http://blog.csdn.net/liuwenbo0920/article/details/7263766 "copy")

 

1. # Hello,Spring Event!!!
2. # the source is:hello
3. # the address is:helloSpring@sina.com
4. # the mail's context is :this is a test eamil content