Spring 事件机制
阅读原文时间:2023年07月08日阅读:3

通过模拟邮件的发送,说明Spring的事件监听机制

事件类

package org.zln.module_chapter2.event;

import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;

/**
* 发送邮件事件类
* Created by sherry on 000005/7/5 22:10.
*/

public class MailSendEvent extends ApplicationContextEvent {

 private String to;  
 public MailSendEvent(ApplicationContext source,String to) {  
     super(source);  
     this.to = to;  
 }

 public String getTo(){  
     return this.to;  
 }  

}

D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\src\org\zln\module_chapter2\event\MailSendEvent.java

事件监听类

package org.zln.module_chapter2.event;

import org.springframework.context.ApplicationListener;

/**
* 邮件发送 监听类
* Created by sherry on 000005/7/5 22:12.
*/

public class MailSendListener implements ApplicationListener {

 /\*对MailSendEvent进行处理\*/  
 @Override  
 public void onApplicationEvent(MailSendEvent mailSendEvent) {  
     MailSendEvent mailSendEvent1 = mailSendEvent;  
     System.out.println("MailSendListener:向"+mailSendEvent1.getTo()+"发送一封邮件");  
 }  

}

D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\src\org\zln\module_chapter2\event\MailSendListener.java

事件动作

package org.zln.module_chapter2.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
* 邮件发送类
* Created by sherry on 000005/7/5 22:15.
*/
public class MailSender implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

 public void sendMail(String to){  
     System.out.println("MailSender:模拟发送邮件");  
     MailSendEvent mailSendEvent = new MailSendEvent(applicationContext,to);  
     /\*向容器中所有事件监听器发送事件\*/  
     applicationContext.publishEvent(mailSendEvent);  
 }  

}

D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\src\org\zln\module_chapter2\event\MailSender.java

事件注册

<!--事件-->  
<bean class="org.zln.module\_chapter2.event.MailSendListener"/>  
<bean id="mailSender" class="org.zln.module\_chapter2.event.MailSender"/>

测试

package org.zln.module_chapter2.event;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/org/zln/cfg/spring/applicationContext.xml"})/*启动Spring容器*/
public class MailSenderTest {

@Autowired  
@Qualifier("mailSender")  
private MailSender mailSender;

@Test  
public void testSetApplicationContext() throws Exception {

}

@Test  
public void testSendMail() throws Exception {  
    mailSender.sendMail("nbzlnzlq@126.com");  
}  

}

D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\test\org\zln\module_chapter2\event\MailSenderTest.java