ApplicationEvent and ApplicationListener encapsulation in Spring, applicationlistener
1. Test Program EventTest. java. To publish an event, you only need to call the FrameEventHolder. publishEvent () method.
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. Application context holder 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. The event class FrameEvent. java inherits from Spring's ApplicationEvent and can maintain the event processing parameters.
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 * keep event service parameters 18 */19 private Object prameter; 20 21/** 22 * Class I event No. 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. Static Method for releasing events 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. event processing interface EventHandler. java
1 package com. junge. spring. event. handler; 2 3 public interface EventHandler {4 5/** 6 * Get event ID 7 */8 String getEventId (); 9 10/** 11 * event handling portal 12*13 * @ param parameter14 */15 void handle (Object parameter); 16 17}
6. the abstract class FrameEventHandler. java of the event processing interface inherits from this class.
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. Implement MailEventHandler. java for business event processing, inherit from FrameEventHandler. java, and implement the handle Method
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 ("emails to be sent" + mail. getEmail () + ", email content:" 19 + mail. getContent (); 20} 21 22} 23 24}
8. The event listener FrameEventListener. java calls the event business processing logic (that is, the handle method)
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. Business VO object 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 business configuration file 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. Test Results
14:23:17 org. springframework. context. support. abstractApplicationContext prepareRefresh information: 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 information: Loading XML bean definitions from class path resource [event. xml] 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 needs to send an email to the 333@email.com with the content of accdddount