深入浅出Spring事件机制

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

事件实现依赖ApplicationEvent抽象类和ApplicationListener接口,applicationContext发布(publishEvent)了事件以后,ApplicationListener的onApplicationEvent监听之:
Java代码如下
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配置如下

	    
	          
	             [email protected]
	             [email protected]
	             [email protected]
	          
	    
	
	
	
	     
	         [email protected]
	     
	

测试类如下
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext5.xml");
		EmailBean emailer = (EmailBean)applicationContext.getBean("emailer");
		emailer.sendEmail("[email protected]", "邮件头", "邮件正文");

打印结果如下:
我已收到通知:[email protected]要发邮件了。。

转载于:https://my.oschina.net/u/218421/blog/37948

你可能感兴趣的:(深入浅出Spring事件机制)