本文概览:介绍了Spring自带事件以及如何自定义一个事件和事件监听器
1 自定义事件
1、自定义一个新的事件
public class MessageEvent extends ApplicationEvent {
private String content;
public MessageEvent(String content) {
super(content);
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
publicclassMessageEventextendsApplicationEvent{
privateStringcontent;
publicMessageEvent(Stringcontent){
super(content);
this.content=content;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
}
2、定义事件监听器。定义的事件监听器会在spring上下文初始化时,自动加载此listener。
@Component
public class MessageEventListener implements ApplicationListener {
public void onApplicationEvent(MessageEvent event) {
System.out.printf("event.message=" + event.getContent());
}
}
1
2
3
4
5
6
@Component
publicclassMessageEventListenerimplementsApplicationListener{
publicvoidonApplicationEvent(MessageEventevent){
System.out.printf("event.message="+event.getContent());
}
}
3、测试发布事件和监听事件
public class Excutor {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
// 发布事件
context.publishEvent(new MessageEvent("执行完成"));
}
}
1
2
3
4
5
6
7
publicclassExcutor{
publicstaticvoidmain(String[]args){
ApplicationContextcontext=newClassPathXmlApplicationContext("classpath:/applicationContext.xml");
// 发布事件
context.publishEvent(newMessageEvent("执行完成"));
}
}
执行结果为:
event.message=执行完成
上述例子项目可以参考:
2 Spring自带事件
目前spring提供常用事件有:
(1)ContextRefreshedEvent,在执行完上下文初始化之后触发这个事件。
类名:AbstractApplicationContext.java
public void refresh() throws BeansException, IllegalStateException {
...
// Last step: publish corresponding event.
finishRefresh();
}
// 发布上下文
protected void finishRefresh() {
...
// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
类名:AbstractApplicationContext.java
publicvoidrefresh()throwsBeansException,IllegalStateException{
...
// Last step: publish corresponding event.
finishRefresh();
}
// 发布上下文
protectedvoidfinishRefresh(){
...
// Publish the final event.
publishEvent(newContextRefreshedEvent(this));
}
(2)ContextStartedEvent,在执行完start之后触发这个事件
类名:AbstractApplicationContext.java
@Override
public void start() {
getLifecycleProcessor().start();
publishEvent(new ContextStartedEvent(this));
}
1
2
3
4
5
6
7
类名:AbstractApplicationContext.java
@Override
publicvoidstart(){
getLifecycleProcessor().start();
publishEvent(newContextStartedEvent(this));
}
(3)ContextStartedEvent,在执行完stop之后触发这个事件
类名:AbstractApplicationContext.java
@Override
public void stop() {
getLifecycleProcessor().stop();
publishEvent(new ContextStoppedEvent(this));
}
1
2
3
4
5
6
7
类名:AbstractApplicationContext.java
@Override
publicvoidstop(){
getLifecycleProcessor().stop();
publishEvent(newContextStoppedEvent(this));
}
(4)ContextClosedEvent,在执行完close之后触发这个事件
@Override
public void close() {
synchronized (this.startupShutdownMonitor) {
doClose();
// If we registered a JVM shutdown hook, we don't need it anymore now:
// We've already explicitly closed the context.
if (this.shutdownHook != null) {
try {
// 通过这个钩子来触发这个close事件
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
}
catch (IllegalStateException ex) {
// ignore - VM is already shutting down
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
publicvoidclose(){
synchronized(this.startupShutdownMonitor){
doClose();
// If we registered a JVM shutdown hook, we don't need it anymore now:
// We've already explicitly closed the context.
if(this.shutdownHook!=null){
try{
// 通过这个钩子来触发这个close事件
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
}
catch(IllegalStateExceptionex){
// ignore - VM is already shutting down
}
}
}
}
事件监听模式包括 事件、监听器、监听器管理器 。可以参考如下