目录
1.知识点
1.1 ApplicationListener接口
1.2 ApplicationEvent抽象类
1.3 ApplicationEventMulticaster
2.代码支撑
2.1 内置Event
2.1.1编写需要监听的listener
2.1.2 测试
2.1.3 执行结果
2.2 自定义Event
2.2.1 自定义event
2.2.2 Listener
2.2.3测试
2.2.4执行结果
此处不讲观察者模式,直接上硬菜…………
小二开始上菜了…………….
1.知识点
1.1 ApplicationListener接口
该接口继承了java.util.EventListener接口
public interface ApplicationListener
void onApplicationEvent(E event);
}
可以看到,只有一个方法onApplicationEvent,我们在自定义得时候,只需要实现这个接口,并且实现onApplicationEvent方法,在方法编写被触发的时候,需要进行处理的业务逻辑
注意:该接口的实现类必须放到IOC容器中,否者是不会起作用的
1.2 ApplicationEvent抽象类
该类定义了事件类型,每个监听器可以通过泛型来设置对某一种或者几种的事件在发生时有反应,作用主要是来进行分类,或者在listener关注的EventObject发生时,可以通过EventObject来向listener传递一些参数,例如事件发生的时间,当时的上下文情况等,可以根据需要重写自己的EventObject类。自定义类型的时候,需要继承该抽象类,并且实现自己的逻辑即可。
Spring中有很多已经实现了的listener
注意:所有的spring事件类型均需要继承该抽象类。
Spring部分内置事件
2.1 ContextRefreshedEvent
ApplicationContext 被初始化或刷新时,该事件被触发。这也可以在 ConfigurableApplicationContext接口中使用 refresh() 方法来触发。
2.2 ContextStartedEvent
当使用 AbstractApplicationContext(ApplicationContext子接口)中的 start() 方法启动 ApplicationContext 时,该事件被发布。可以在listener中做一些初始化的操作
2.3 ContextStoppedEvent
当使用AbstractApplicationContex中的stop()停止ApplicationContext 时,发布这个事件。可以在listener进行释放资源的操作。
2.4 ContextClosedEvent
当使用 AbstractApplicationContext接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。
1.3 ApplicationEventMulticaster
该接口定义了如何去管理和发布事件
public interface ApplicationEventMulticaster {
/**
* Add a listener to be notified of all events.
* @param listener the listener to add
*/
void addApplicationListener(ApplicationListener> listener);
/**
* Add a listener bean to be notified of all events.
* @param listenerBeanName the name of the listener bean to add
*/
void addApplicationListenerBean(String listenerBeanName);
/**
* Remove a listener from the notification list.
* @param listener the listener to remove
*/
void removeApplicationListener(ApplicationListener> listener);
/**
* Remove a listener bean from the notification list.
* @param listenerBeanName the name of the listener bean to add
*/
void removeApplicationListenerBean(String listenerBeanName);
/**
* Remove all listeners registered with this multicaster.
*
After a remove call, the multicaster will perform no action
* on event notification until new listeners are being registered.
*/
void removeAllListeners();
/**
* Multicast the given application event to appropriate listeners.
*
Consider using {@link #multicastEvent(ApplicationEvent, ResolvableType)}
* if possible as it provides a better support for generics-based events.
* @param event the event to multicast
*/
void multicastEvent(ApplicationEvent event);
/**
* Multicast the given application event to appropriate listeners.
*
If the {@code eventType} is {@code null}, a default type is built
* based on the {@code event} instance.
* @param event the event to multicast
* @param eventType the type of event (can be null)
* @since 4.2
*/
void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType);
}
其下有很多实现类,spring主要使用了其中的SimpleApplicationEventMulticaster
2.代码支撑
2.1 内置Event
2.1.1编写需要监听的listener
import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextStoppedEvent;import org.springframework.stereotype.Component;/** * 1.实现ApplicationListener,并且传入需要关注的事件类型,如果不传的话,所有的事件在触发时都会被触发 * 2.实现onApplicationEvent方法,可以通过event来获取数据 * 3.将其注入到容器中,此处实现@Component * @author yx * */@Componentpublic class MyStopListener implements ApplicationListener
2.1.2 测试
import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.AbstractApplicationContext; import com.yangxiao.listener.config.MyConfig; public class ListenerTest { @Test public void testNotifyListener() { ApplicationContext ctx=new AnnotationConfigApplicationContext(MyConfig.class); ((AbstractApplicationContext) ctx).stop(); }}
2.1.3 执行结果
2.2 自定义Event
2.2.1 自定义event
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;
public class NotifyEvent extends ApplicationContextEvent{
//自定义定义一个领导
private String caller;
public NotifyEvent(ApplicationContext source,String caller) {
super(source);
this.caller=caller;
}
public String getCaller() {
return caller;
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
2.2.2 Listener
import com.yangxiao.event.NotifyEvent;
/**
* 1.实现ApplicationListener,并且传入需要关注的事件类型,如果不传的话,所有的事件在触发时都会被触发
* 2.实现onApplicationEvent方法,可以通过event来获取数据
* 3.将其注入到容器中,此处实现@Component
* @author yx
*
*/
@Component
public class NotifyUserListener implements ApplicationListener
@Override
public void onApplicationEvent(NotifyEvent event) {
String caller=event.getCaller();
System.out.println(caller+"来叫大家干活了.......");
}
}
2.2.3测试
import com.yangxiao.event.NotifyEvent;
import com.yangxiao.listener.config.MyConfig;
public class ListenerTest {
@Test
public void testStopListener() {
ApplicationContext ctx=new AnnotationConfigApplicationContext(MyConfig.class);
((AbstractApplicationContext) ctx).stop();
}
@Test
public void testNotifyListener() {
ApplicationContext ctx=new AnnotationConfigApplicationContext(MyConfig.class);
String caller="马云";
ctx.publishEvent(new NotifyEvent(ctx, caller));
}
}
2.2.4执行结果