码迷,mamicode.com
首页 > 移动开发 > 详细

Spring中ApplicationEvent和ApplicationListener封装

时间:2015-01-23 16:01:47      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

1.测试程序EventTest.java,发布一个事件只需要调用FrameEventHolder.publishEvent()方法即可。

 1 package com.junge.spring.event;
 2 
 3 import com.junge.spring.vo.Mail;
 4 
 5 public class EventTest {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         FrameEvent frameEvent = new FrameEvent();
12 
13         Mail mail = new Mail();
14         mail.setContent("accdddount");
15         mail.setEmail("333@email.com");
16 
17         frameEvent.setPrameter(mail);
18         frameEvent.setEventId("send_mail_notify");
19         frameEvent.setSync(false);
20 
21         FrameEventHolder.publishEvent(frameEvent);
22     }
23 
24 }

 

2.应用上下文保持器ContextHolder.java

 1 /**
 2  * 
 3  */
 4 package com.junge.spring.context;
 5 
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 /**
10  * @author Administrator
11  * 
12  */
13 public class ContextHolder {
14 
15     private static ApplicationContext context = null;
16 
17     static {
18         context = new ClassPathXmlApplicationContext("event.xml");
19     }
20 
21     public static ApplicationContext getApplicationContext() {
22         return context;
23     }
24 }

3.事件类FrameEvent.java,继承自Spring的ApplicationEvent,可以保持事件处理参数

 1 /**
 2  * 
 3  */
 4 package com.junge.spring.event;
 5 
 6 import org.springframework.context.ApplicationEvent;
 7 
 8 /**
 9  * @author Administrator
10  * 
11  */
12 public class FrameEvent extends ApplicationEvent {
13 
14     private static final long serialVersionUID = 1L;
15 
16     /**
17      * 保持事件业务参数
18      */
19     private Object prameter;
20 
21     /**
22      * 一类事件编号
23      */
24     private String eventId;
25 
26     public FrameEvent() {
27         super("source");
28     }
29 
30     public FrameEvent(Object source) {
31         super(source);
32     }
33 
34     public Object getPrameter() {
35         return prameter;
36     }
37 
38     public void setPrameter(Object prameter) {
39         this.prameter = prameter;
40     }
41 
42     public String getEventId() {
43         return eventId;
44     }
45 
46     public void setEventId(String eventId) {
47         this.eventId = eventId;
48     }
49 
50 }

4.发布事件静态方法 FrameEventHolder.java

package com.junge.spring.event;

import org.springframework.util.Assert;

import com.junge.spring.context.ContextHolder;

public class FrameEventHolder {

	public static void publishEvent(FrameEvent frameEvent) {

		Assert.notNull(frameEvent);

		ContextHolder.getApplicationContext().publishEvent(frameEvent);
	}

}

 5.事件处理接口EventHandler.java

 1 package com.junge.spring.event.handler;
 2 
 3 public interface EventHandler {
 4 
 5     /**
 6      * 获取事件ID
 7      */
 8     String getEventId();
 9 
10     /**
11      * 处理事件入口
12      * 
13      * @param parameter
14      */
15     void handle(Object parameter);
16 
17 }

6.事件处理接口抽象类FrameEventHandler.java,业务事件处理从该类继承

 1 /**
 2  * 
 3  */
 4 package com.junge.spring.event.handler;
 5 
 6 /**
 7  * @author Administrator
 8  * 
 9  */
10 public abstract class FrameEventHandler implements EventHandler {
11 
12     private String eventId;
13 
14     /**
15      * @param eventId
16      *            the eventId to set
17      */
18     public void setEventId(String eventId) {
19         this.eventId = eventId;
20     }
21 
22     /*
23      * (non-Javadoc)
24      * 
25      * @see com.junge.spring.event.listener.EventHandler#getEventId()
26      */
27     @Override
28     public String getEventId() {
29 
30         return eventId;
31     }
32 }

7.业务事件处理实现MailEventHandler.java,从FrameEventHandler.java继承,实现handle方法

 1 /**
 2  * 
 3  */
 4 package com.junge.spring.event.handler;
 5 
 6 import com.junge.spring.vo.Mail;
 7 
 8 /**
 9  * @author Administrator
10  * 
11  */
12 public class MailEventHandler extends FrameEventHandler {
13 
14     @Override
15     public void handle(Object parameter) {
16         if (null != parameter) {
17             Mail mail = (Mail) parameter;
18             System.out.println("需要发送邮件给" + mail.getEmail() + ",邮件内容是:"
19                     + mail.getContent());
20         }
21 
22     }
23 
24 }

8.事件监听器FrameEventListener.java,调用事件业务处理逻辑(即调用handle方法)

 1 /**
 2  * 
 3  */
 4 package com.junge.spring.event.listener;
 5 
 6 import org.springframework.context.ApplicationListener;
 7 
 8 import com.junge.spring.event.FrameEvent;
 9 import com.junge.spring.event.handler.EventHandler;
10 
11 /**
12  * @author Administrator
13  * 
14  */
15 public class FrameEventListener implements ApplicationListener<FrameEvent> {
16 
17     private EventHandler eventHandler;
18 
19     @Override
20     public void onApplicationEvent(FrameEvent event) {
21 
22         if (eventHandler.getEventId().equals(event.getEventId())) {
23             eventHandler.handle(event.getPrameter());
24         }
25     }
26 
27     public EventHandler getEventHandler() {
28         return eventHandler;
29     }
30 
31     public void setEventHandler(EventHandler eventHandler) {
32         this.eventHandler = eventHandler;
33     }
34 
35 }

9.业务VO对象Mail.java

 1 /**
 2  * 
 3  */
 4 package com.junge.spring.vo;
 5 
 6 /**
 7  * @author Administrator
 8  *
 9  */
10 public class Mail {
11     private String email;
12     
13     private String content;
14 
15     public String getEmail() {
16         return email;
17     }
18 
19     public void setEmail(String email) {
20         this.email = email;
21     }
22 
23     public String getContent() {
24         return content;
25     }
26 
27     public void setContent(String content) {
28         this.content = content;
29     }
30 }

10.Spring业务配置文件event.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <p:beans xmlns:p="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans spring-beans-3.2.xsd ">
 5 
 6     <p:bean id="mailHandler" class="com.junge.spring.event.handler.MailEventHandler">
 7         <p:property name="eventId" value="send_mail_notify" />
 8     </p:bean>
 9 
10     <p:bean id="mailListener" class="com.junge.spring.event.listener.FrameEventListener">
11         <p:property name="eventHandler" ref="mailHandler" />
12     </p:bean>
13 </p:beans>

11.测试结果

2015-1-23 14:23:17 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2808b3: startup date [Fri Jan 23 14:23:17 CST 2015]; root of context hierarchy
2015-1-23 14:23:18 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [event.xml]
2015-1-23 14:23:19 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@db4fa2: defining beans [mailHandler,mailListener]; root of factory hierarchy
需要发送邮件给333@email.com,邮件内容是:accdddount

 

Spring中ApplicationEvent和ApplicationListener封装

标签:

原文地址:http://www.cnblogs.com/junge8618/p/4244136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!