Event
Spring 事件为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后,希望另外一个 Bean 知道并能做相应的处理,这时我们就需要让另外一个 Bean 监听当前 Bean 所发送的事件.
Spring 的事件需要遵循如下流程:
- 自定义事件,继承 ApplicationEvent.
- 定义事件监听器,实现 ApplicationListener.
- 使用容器发布事件.
示例:
package com.pangu.event;
import org.springframework.context.ApplicationEvent;
/**
* @ClassName: DemoEvent
* @Description: TODO 自定义事件
* @author etfox
* @date 2018年8月22日 下午10:21:40
*
* @Copyright: 2018 www.etfox.com Inc. All rights reserved.
*/
public class DemoEvent extends ApplicationEvent {
/**
* @Fields field:field:{todo}
*/
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;
}
}
package com.pangu.event;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @ClassName: DemoListener
* @Description: TODO 事件监听
* @author etfox
* @date 2018年8月22日 下午10:25:18
*
* @Copyright: 2018 www.etfox.com Inc. All rights reserved.
*/
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {
@Override
public void onApplicationEvent(DemoEvent event) {
String msg = event.getMsg();
System.out.println("我(bean-demoListener)接收了 bean-demoPublisher 发布的消息:"+ msg);
}
}
package com.pangu.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class DemoPublisher {
@Autowired
ApplicationContext applicationContext;
//发布
public void publish(String msg){
applicationContext.publishEvent(new DemoEvent(this, msg));
}
}
package com.pangu.event;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.pangu.event")
public class EventConfig {
}
package com.pangu.event;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
@Test
public void run(){
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(EventConfig.class);
DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
demoPublisher.publish("hello application event.");
context.close();
}
}
运行结果:
八月 23, 2018 9:20:33 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@762efe5d: startup date [Thu Aug 23 09:20:33 CST 2018]; root of context hierarchy
我(bean-demoListener)接收了 bean-demoPublisher 发布的消息:hello application event.
八月 23, 2018 9:20:33 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@762efe5d: startup date [Thu Aug 23 09:20:33 CST 2018]; root of context hierarchy