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

Spring 事件(Application Event)

2017-03-14 14:42 561 查看
Spring的事件(Application Event)为Bean与Bean之间的消息通信提供了支持。当一个Bean处理完一个任务后,希望从另一个知道并做相应处理,这个时候我们就需要让另一个Bean监听当前Bean所发送的事件。

Spring的事件需要遵循如下流程:

自定义事件,继承ApplicationEvent。
定义事件监听器,实现ApplicationListener。
使用容器发布事件。
(1)自定义事件:
package com.chenfeng.xiaolyuh.event.custom_event;

import org.springframework.context.ApplicationEvent;

/**
*
* @ClassName DemoEvent
* @Description 自定义事件
* @author yuhao.wang
* @Date 2017年3月14日 下午1:54:57
* @version 1.0.0
*/
public class DemoEvent extends ApplicationEvent {

private static final long serialVersionUID = 1L;
private String msg;

public DemoEvent(Object source, String msg) {
super(source);
this.msg = msg;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
}


(2)定义事件监听器:
package com.chenfeng.xiaolyuh.event.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.chenfeng.xiaolyuh.event.custom_event.DemoEvent;

/**
*
* @ClassName DemoListener
* @Description 事件监听器,实现ApplicationListener的接口并指定监听的事件类型。
* @author yuhao.wang
* @Date 2017年3月14日 下午1:59:50
* @version 1.0.0
*/
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {

@Override
// 使用onApplicationEvent方法对消息进行接收处理。
public void onApplicationEvent(DemoEvent event) {
String msg = event.getMsg();
System.out.println("我(bean-demoListener)接收到了Bean-demoPublisher发布的消息:" + msg);
}

}


(3)使用容器发布事件:
package com.chenfeng.xiaolyuh.event.publisher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.chenfeng.xiaolyuh.event.custom_event.DemoEvent;

/**
*
* @ClassName DemoPublisher
* @Description 发布事件
* @author yuhao.wang
* @Date 2017年3月14日 下午2:14:20
* @version 1.0.0
*/
@Component
public class DemoPublisher {
@Autowired
// 注入容器applicationContext用来发布事件
ApplicationContext applicationContext;

public void publish(String msg){
// 使用applicationContext的publishEvent方法来发布。
applicationContext.publishEvent(new DemoEvent(this, msg));
}
}


(4)配置类:
package com.chenfeng.xiaolyuh.event.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration // 声明当前类是一个配置类,相当于Spring配置的XML文件
@ComponentScan(basePackages={"com.chenfeng.xiaolyuh.event"})
public class EventConfig {

}


(5)测试类:
package com.chenfeng.xiaolyuh.test;

import org.junit.After;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.chenfeng.xiaolyuh.event.config.EventConfig;
import com.chenfeng.xiaolyuh.event.publisher.DemoPublisher;

/**
* Created by yuhao.wang on 2017/3/9.
*/
public class SpringEventTest {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);

@Test
public void contextTest() {
DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
demoPublisher.publish("Hello Application Event");
}

@After
public void closeContext() {
context.close();
}

}


(6)结果:
三月 14, 2017 2:43:06 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ccd43c2: startup date [Tue Mar 14 14:43:06 CST 2017]; root of context hierarchy
我(bean-demoListener)接收到了Bean-demoPublisher发布的消息:Hello Application Event
三月 14, 2017 2:43:06 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ccd43c2: startup date [Tue Mar 14 14:43:06 CST 2017]; root of context hierarchy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息