package com.uqee.spring.applicationContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationEvent; public class EmailEvent extends ApplicationEvent { private String address; private String text; private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } private Log logger = LogFactory.getLog(EmailEvent.class); public EmailEvent(Object source) { super(source); // TODO Auto-generated constructor stub } public EmailEvent(String email,String address,String text) { super(email); this.email = email; this.address = address; this.text = text; } public void printInfo() { // System.out.println("Send this Email, address:"+address+" text:"); logger.info("Send this Email, address:"+address+" text:"+text); } /** * */ private static final long serialVersionUID = 1667085258090884727L; }
package com.uqee.spring.applicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; public class EmailNotifier implements ApplicationListener { private String notificationAddress; public String getNotificationAddress() { return notificationAddress; } public void setNotificationAddress(String notificationAddress) { this.notificationAddress = notificationAddress; } @Override public void onApplicationEvent(ApplicationEvent event) { // TODO Auto-generated method stub if(event instanceof EmailEvent) { //notifier apppriate person EmailEvent emailEvent = (EmailEvent)event; System.out.println("我已收到通知:"+emailEvent.getEmail()+"要發郵件了。。"); } } }
package com.uqee.spring.applicationContext; import java.util.List; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class EmailBean implements ApplicationContextAware { private ApplicationContext ctx = null; public ApplicationContext getCtx() { return ctx; } private List backList; public List getBackList() { return backList; } public void setBackList(List backList) { this.backList = backList; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.ctx = applicationContext; } public void sendEmail(String email,String title,String text) { if(backList.contains(email)) { EmailEvent evt = new EmailEvent(email,title, text); ctx.publishEvent(evt); return ; } } }
xml配置以下:
<bean id="emailer" class="com.uqee.spring.applicationContext.EmailBean"> <property name="backList"> <list> <value>ncg2.0@163.com</value> <value>niechanggang@gmail.com</value> <value>373045912@qq.com</value> </list> </property> </bean> <bean id="emailNotifier" class="com.uqee.spring.applicationContext.EmailNotifier"> <property name="notificationAddress"> <value>qingwa@163.com</value> </property> </bean>
測試類以下:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext5.xml"); EmailBean emailer = (EmailBean)applicationContext.getBean("emailer"); emailer.sendEmail("373045912@qq.com", "郵件頭", "郵件正文");
打印結果以下: