首页 > 代码库 > Spring中ApplicationEvent和ApplicationListener封装

Spring中ApplicationEvent和ApplicationListener封装

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 Administrator11  * 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 Administrator10  * 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 parameter14      */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 eventId16      *            the eventId to set17      */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     @Override28     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 Administrator10  * 11  */12 public class MailEventHandler extends FrameEventHandler {13 14     @Override15     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 Administrator13  * 14  */15 public class FrameEventListener implements ApplicationListener<FrameEvent> {16 17     private EventHandler eventHandler;18 19     @Override20     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="http://www.mamicode.com/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 hierarchy2015-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封装