spring event是spring的事件机制,和gui中的事件类似,使用的也是观察者模式。spring event为bean与bean之间的消息通信提供了支持。当一个bean处理完一个任务后,希望另外一个bean知道并能做响应的处理,这时我们就需要让另一个bean监听当前bean所发送的事件。
这里有三个角色
事件发布者:消息发发布者
事件订阅者:可以有多个,订阅消息的人
事件:发生的事件,或者传递的信息
三个角色非常好理解,事件的发布者就是发布时间的人,也就是事件源,我们以gui中的按钮为例,当点击按钮的时候触发一个点击事件。这个过程中,按钮就是时间源,点击事件就是一个事件,记录的是点击的一些状态,那么点击的处理或者响应就是事件的订阅者,订阅者得到响应以后,处理一些事情。
理解清楚了这三种角色,下面来具体看代码的实现,都非常的简单
先定义事件
package com.hy.spring.test5;
import org.springframework.context.ApplicationEvent;
public class DemoEvent extends ApplicationEvent {
private static final long serialVersionUID = -1916624710173721661L;
private String msg;
public DemoEvent(Object resource, String msg) {
super(resource);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
再来定义第一个订阅者(订阅者是通过实现ApplicationListener<DemoEvent> 接口,并指定泛型参数来确定要监听的事件的,这个过程spring会给我加入到观察者的队列中)
package com.hy.spring.test5;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener1 implements ApplicationListener<DemoEvent> {
@Override
public void onApplicationEvent(DemoEvent event) {
String msg = event.getMsg();
System.out.println("1收到消息 " + msg);
}
}
再来定义第二个订阅者
package com.hy.spring.test5;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener2 implements ApplicationListener<DemoEvent> {
@Override
public void onApplicationEvent(DemoEvent event) {
String msg = event.getMsg();
System.out.println("2收到消息 " + msg);
}
}
再来定义发布者
package com.hy.spring.test5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class DemoPublisher {
@Autowired
private ApplicationContext applicationContext;
public void publish(String msg) {
applicationContext.publishEvent(new DemoEvent(this, msg));
}
}
角色都定义好了,下面是spring的配置类
package com.hy.spring.test5;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.hy.spring.test5")
public class EventConfig {
}
测试类
package com.hy.spring.test5;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
DemoPublisher p = context.getBean(DemoPublisher.class);
p.publish("hello");
context.close();
}
}
运行结果
七月 19, 2017 4:06:52 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e2c634b: startup date [Wed Jul 19 16:06:52 CST 2017]; root of context hierarchy
1收到消息 hello
2收到消息 hello
七月 19, 2017 4:06:53 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e2c634b: startup date [Wed Jul 19 16:06:52 CST 2017]; root of context hierarchy