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();
		
	}
}

運行結果。

自定義事件監聽就完成了,啓動流程裏該說的都說了,所以這後面就只是簡單的記錄一下。

 

 

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