1.测试程序EventTest.java,发布一个事件只需要调用FrameEventHolder.publishEvent()方法即可。
package com.junge.spring.event; import com.junge.spring.vo.Mail; public class EventTest { /**
* @param args
*/
public static void main(String[] args) {
FrameEvent frameEvent = new FrameEvent(); Mail mail = new Mail();
mail.setContent("accdddount");
mail.setEmail("333@email.com"); frameEvent.setPrameter(mail);
frameEvent.setEventId("send_mail_notify");
frameEvent.setSync(false); FrameEventHolder.publishEvent(frameEvent);
} }
2.应用上下文保持器ContextHolder.java
/**
*
*/
package com.junge.spring.context; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Administrator
*
*/
public class ContextHolder { private static ApplicationContext context = null; static {
context = new ClassPathXmlApplicationContext("event.xml");
} public static ApplicationContext getApplicationContext() {
return context;
}
}
3.事件类FrameEvent.java,继承自Spring的ApplicationEvent,可以保持事件处理参数
/**
*
*/
package com.junge.spring.event; import org.springframework.context.ApplicationEvent; /**
* @author Administrator
*
*/
public class FrameEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; /**
* 保持事件业务参数
*/
private Object prameter; /**
* 一类事件编号
*/
private String eventId; public FrameEvent() {
super("source");
} public FrameEvent(Object source) {
super(source);
} public Object getPrameter() {
return prameter;
} public void setPrameter(Object prameter) {
this.prameter = prameter;
} public String getEventId() {
return eventId;
} public void setEventId(String eventId) {
this.eventId = eventId;
} }
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
package com.junge.spring.event.handler; public interface EventHandler { /**
* 获取事件ID
*/
String getEventId(); /**
* 处理事件入口
*
* @param parameter
*/
void handle(Object parameter); }
6.事件处理接口抽象类FrameEventHandler.java,业务事件处理从该类继承
/**
*
*/
package com.junge.spring.event.handler; /**
* @author Administrator
*
*/
public abstract class FrameEventHandler implements EventHandler { private String eventId; /**
* @param eventId
* the eventId to set
*/
public void setEventId(String eventId) {
this.eventId = eventId;
} /*
* (non-Javadoc)
*
* @see com.junge.spring.event.listener.EventHandler#getEventId()
*/
@Override
public String getEventId() { return eventId;
}
}
7.业务事件处理实现MailEventHandler.java,从FrameEventHandler.java继承,实现handle方法
/**
*
*/
package com.junge.spring.event.handler; import com.junge.spring.vo.Mail; /**
* @author Administrator
*
*/
public class MailEventHandler extends FrameEventHandler { @Override
public void handle(Object parameter) {
if (null != parameter) {
Mail mail = (Mail) parameter;
System.out.println("需要发送邮件给" + mail.getEmail() + ",邮件内容是:"
+ mail.getContent());
} } }
8.事件监听器FrameEventListener.java,调用事件业务处理逻辑(即调用handle方法)
/**
*
*/
package com.junge.spring.event.listener; import org.springframework.context.ApplicationListener; import com.junge.spring.event.FrameEvent;
import com.junge.spring.event.handler.EventHandler; /**
* @author Administrator
*
*/
public class FrameEventListener implements ApplicationListener<FrameEvent> { private EventHandler eventHandler; @Override
public void onApplicationEvent(FrameEvent event) { if (eventHandler.getEventId().equals(event.getEventId())) {
eventHandler.handle(event.getPrameter());
}
} public EventHandler getEventHandler() {
return eventHandler;
} public void setEventHandler(EventHandler eventHandler) {
this.eventHandler = eventHandler;
} }
9.业务VO对象Mail.java
/**
*
*/
package com.junge.spring.vo; /**
* @author Administrator
*
*/
public class Mail {
private String email; private String content; public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}
10.Spring业务配置文件event.xml
<?xml version="1.0" encoding="UTF-8"?>
<p:beans xmlns:p="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans spring-beans-3.2.xsd "> <p:bean id="mailHandler" class="com.junge.spring.event.handler.MailEventHandler">
<p:property name="eventId" value="send_mail_notify" />
</p:bean> <p:bean id="mailListener" class="com.junge.spring.event.listener.FrameEventListener">
<p:property name="eventHandler" ref="mailHandler" />
</p:bean>
</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