您的位置:首页 > 移动开发

ApplicationContext的事件机制

2018-02-05 00:00 453 查看
ApplicationContext的事件机制是观察者模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationConext的事件处理。如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEven时,ApplicationListener Bean将自动被触发。

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

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

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

Spring的事件机制与所有事件机制都基本相似,它们都需要事件源、事件和事件监听器组成。此处的事件源是ApplicationConetext,并且事件必须由Java程序显示触发。

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

下面定义一个邮件事件:

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中配置邮件事件:

<!-- 配置邮件监听器 -->
<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容器时也会自动触发容器事件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息