1 介绍
/**
* Interface to be implemented by application event listeners.
* Based on the standard {@code java.util.EventListener} interface
* for the Observer design pattern.
*
* <p>As of Spring 3.0, an ApplicationListener can generically declare the event type
* that it is interested in. When registered with a Spring ApplicationContext, events
* will be filtered accordingly, with the listener getting invoked for matching event
* objects only.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @param <E> the specific ApplicationEvent subclass to listen to
* @see org.springframework.context.event.ApplicationEventMulticaster
*/
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
用于监听容器中发布的事件(继承ApplicationEvent)
2 测试spring默认发布的事件
实现ApplicationListener接口
package study.wyy.spring.extend.applicationListener.spi;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:06 下午
*/
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("收到事件: " + event);
}
}
配置
package study.wyy.spring.extend.applicationListener.config;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import study.wyy.spring.extend.applicationListener.spi.MyApplicationListener;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:08 下午
*/
@Configuration
public class SpringConfig {
@Bean
public ApplicationListener myApplicationListener(){
return new MyApplicationListener();
}
}
测试
@org.junit.Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
context.close();
}
输出
收到事件: org.springframework.context.event.ContextRefreshedEvent
收到事件: org.springframework.context.event.ContextClosedEvent
默认会发布两个事件:
- 容器刷新完成事件:所有的bean都完全创建会发布这个事件
- 容器关闭事件:容器关闭的时候会发布的事件
我们可以监听这两个事件,进行一些处理:
package study.wyy.spring.extend.applicationListener.spi;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:06 下午
*/
public class MyApplicationListener2 implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ContextRefreshedEvent){
System.out.println("容器刷新时候要处理。。。。。。");
}
if(event instanceof ContextClosedEvent){
// 容器关闭的时候处理
System.out.println("容器关闭时候要处理。。。。。。");
}
}
}
3 自定义事件发布和监听
自定义事件,继承ApplicationEvent
package study.wyy.spring.extend.applicationListener.event;
import org.springframework.context.ApplicationEvent;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:17 下午
*/
public class ConsumerEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public ConsumerEvent(Object source) {
super(source);
}
}
定义一个监听器监听ConsumerEvent
package study.wyy.spring.extend.applicationListener.spi;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import study.wyy.spring.extend.applicationListener.event.ConsumerEvent;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:06 下午
*/
public class MyApplicationListener3 implements ApplicationListener<ConsumerEvent> {
@Override
public void onApplicationEvent(ConsumerEvent event) {
System.out.println("收到事件:" + event.getSource());
}
}
发布事件
@org.junit.Test
public void test03(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig2.class);
ConsumerEvent event = new ConsumerEvent("自定义事件");
context.publishEvent(event);
context.close();
}
输出
收到事件:自定义事件
3 @EventListener
可以使用@EventListener
来监听事件,不需要通过实现ApplicationListener接口
package study.wyy.spring.extend.applicationListener.event;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:26 下午
*/
public class ConsumerEventListener {
// classes属性指定要监听的事件类型
@EventListener(classes = {ConsumerEvent.class})
public void listen(ConsumerEvent event){
System.out.println("收到事件:" + event.getSource());
}
}
配置类
package study.wyy.spring.extend.applicationListener.config;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import study.wyy.spring.extend.applicationListener.event.ConsumerEventListener;
import study.wyy.spring.extend.applicationListener.spi.MyApplicationListener3;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:08 下午
*/
@Configuration
public class SpringConfig3 {
@Bean
public ConsumerEventListener consumerEventListener(){
return new ConsumerEventListener();
}
}
测试
@org.junit.Test
public void test04(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig3.class);
ConsumerEvent event = new ConsumerEvent("自定义事件。。。。");
context.publishEvent(event);
context.close();
}
输出
收到事件:自定义事件。。。。