ApplicationContext的事件机制

    ApplicationContext的事件机制是观察者模式的实现,经过ApplicationEvent类和ApplicationListener接口,能够实现ApplicationConext的事件处理。若是容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEven时,ApplicationListener Bean将自动被触发。java

    Spring的事件框架有以下两个成员:spring

    1)ApplicationEvent:容器事件,必须由ApplicationContext发布。app

    2)ApplicationListener:监听器,可由容器中的任何监听器承担。框架

    Spring的事件机制与全部事件机制都基本类似,它们都须要事件源、事件和事件监听器组成。此处的事件源是ApplicationConetext,而且事件必须由Java程序显示触发。ide

    onApplicationEvent(ApplictionEvent event);每当容器内发送任何事件时,此方法都被触发;this

 下面定义一个邮件事件:code

package com.custle.spring;

import org.springframework.context.ApplicationEvent;

public class EmailEvent extends ApplicationEvent {

	private String address;
	private String text;

	public EmailEvent(Object source) {
		super(source);
	}

	public EmailEvent(Object source, String address, String text) {
		super(source);
		this.address = address;
		this.text = text;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

}
package com.custle.spring;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

public class EmailNotifiter implements ApplicationListener {

	//该方法会在容器发送时自动触发
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if(event instanceof EmailEvent){
			//只处理EmailEvent,发送email通知
			EmailEvent emailEvent = (EmailEvent)event;
			System.out.println("须要发送邮件的接收地址:"+emailEvent.getAddress());
		}else{
			//容器内置事件不做处理
			System.out.println("容器自己的事件: "+ event);
		}
		
	}

}

在applicationContext-spring.xml中配置邮件事件:xml

<!-- 配置邮件监听器 -->
<bean id="emailNotifiter" class="com.custle.spring.EmailNotifiter" />

经过ClassPathXmlApplicationContext读取配置文件:接口

package com.custle.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmailTest {

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-spring.xml");
		EmailEvent emailEvent = new EmailEvent("hello", "526295149@qq.com", "send email");
		//将EmailEvent事件添加至ApplicationContext容器
		applicationContext.publishEvent(emailEvent);
	}

}

控制台输出:事件

2018-02-05 13:24:27  INFO (AbstractApplicationContext.java:513)  - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@22f4bf02: startup date [Mon Feb 05 13:24:27 CST 2018]; root of context hierarchy
2018-02-05 13:24:27  INFO (XmlBeanDefinitionReader.java:316)  - Loading XML bean definitions from class path resource [applicationContext-spring.xml]
容器自己的事件: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@22f4bf02: startup date [Mon Feb 05 13:24:27 CST 2018]; root of context hierarchy]
须要发送邮件的接收地址:526295149@qq.com

上文是经过ApplicationContext的pulishEvent()来主动触发容器事件,其实当系统建立Spring容器、加载Spring容器时也会自动触发容器事件。

相关文章
相关标签/搜索