SpringBoot(十六)自定义事件监听

SpringBoot版本:2.1.1

相信走完SpringBoot2.1.1启动流程分析以后,对事件应该是很熟悉了。

监听实现方式

1、在SpringApplication的构造方法中就会从META-INF/spring.factories得到ApplicationListener的实现类对象集合。

2、在发布ApplicationEnvironmentPreparedEvent事件时DelegatingApplicationListener会得到配置项"context.listener.classes"设置的listener,得到实例对象添加到多播器multicaster中。

3、在刷新应用上下文refreshContext()调用BeanFactoryPostProcessor接口实现类重写的postProcessBeanFactory()方法时,EventListenerMethodProcessor会得到一个DefaultEventListenerFactory,该类实现了SmartInitializingSingleton接口,在所有单实例bean实例化之后,会回调afterSingletonsInstantiated()方法,该方法中会处理带有@EventListener注解的方法,使用DefaultEventListenerFactory创建Listener,并添加到上下文

所以事件监听的实现方式这里就已经有三种了。

4、还有一种就是通过SprngApplication对象的addListeners()方法添加Listener,该方法添加的Listener最终不过也是添加到了上面说的第1点中得到的ApplicationListener实现类对象集合中。

最后是有四种方式是实现事件监听,其实差不多也就是三种,第4种也说了其实是添加到了第1点中得到的ApplicationListener实现类对象集合中。

示例代码

首先编写一个自定义事件,继承ApplicationEvent。

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {

	private static final long serialVersionUID = 1L;

	public CustomEvent(Object source) {
		super(source);
	}

}

在编写一个Listener。

import org.springframework.context.ApplicationListener;

import com.eastcom.event.CustomEvent;

public class CustomListener implements ApplicationListener<CustomEvent> {

	@Override
	public void onApplicationEvent(CustomEvent event) {
		String ource = (String) event.getSource();
		System.out.println(this.getClass().getSimpleName()+"监听到:"+ource);
	}

}

第4种方式addListeners()方法

先说第4种方式

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(Application.class);添加
                //添加Listener
		application.addListeners(new CustomListener());
		ConfigurableApplicationContext context = application.run(args);
                //发布事件,调用就是AbstractApplicationContext的publishEvent()方法
		context.publishEvent(new CustomEvent("CustomEvent事件"));
		context.close();
		
	}
}

运行结果。

第1种方式注册到META-INF/spring.factories

在resources目录下新建META-INF/spring.factories文件,使用ApplicationListener全类名做key,我们自定义的Listener全类名做value,文件中的类容如下。

 移除addListeners()方法。

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(Application.class);
		ConfigurableApplicationContext context = application.run(args);
		context.publishEvent(new CustomEvent("CustomEvent事件"));
		context.close();
		
	}
}

运行结果。

第2种方式context.listener.classes配置项

记得将spring.factories文件中的注释掉。

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(Application.class);
		ConfigurableApplicationContext context = application.run(args);
		context.publishEvent(new CustomEvent("CustomEvent事件"));
		context.close();
		
	}
}

运行结果。

第3种方式@EventListener注解方式

新建一个Listener,纳入spring容器,前面的设置都注释掉。

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import com.eastcom.event.CustomEvent;

@Component
public class AnnotationListener {
	
	@EventListener
	public void onApplication(CustomEvent event) {
		String ource = (String) event.getSource();
		System.out.println(this.getClass().getSimpleName()+"监听到:"+ource);
	}
}
@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(Application.class);
		ConfigurableApplicationContext context = application.run(args);
		context.publishEvent(new CustomEvent("CustomEvent事件"));
		context.close();
		
	}
}

运行结果。

自定义事件监听就完成了,启动流程里该说的都说了,所以这后面就只是简单的记录一下。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章