spring listener详尽篇

目录

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<E extends ApplicationEvent> extends EventListener {

    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.

     * <p>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.

     * <p>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.

     * <p>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

  1. import org.springframework.context.ApplicationListener;
  2. import org.springframework.context.event.ContextStoppedEvent;
  3. import org.springframework.stereotype.Component;
  4. /**
  5.  * 1.实现ApplicationListener,并且传入需要关注的事件类型,如果不传的话,所有的事件在触发时都会被触发
  6.  * 2.实现onApplicationEvent方法,可以通过event来获取数据
  7.  * 3.将其注入到容器中,此处实现@Component
  8.  * @author yx
  9.  *
  10.  */
  11. @Component
  12. public class MyStopListener implements ApplicationListener<ContextStoppedEvent>{
  13.     @Override
  14.     public void onApplicationEvent(ContextStoppedEvent event) {
  15.         System.out.println("IOC容器执行了stop方法...........");
  16.     }
  17. }

2.1.2 测试

  1. import org.junit.Test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. import org.springframework.context.support.AbstractApplicationContext;
  5.  
  6. import com.yangxiao.listener.config.MyConfig;
  7.  
  8. public class ListenerTest {
  9.     @Test
  10.     public void testNotifyListener() {
  11.         ApplicationContext ctx=new AnnotationConfigApplicationContext(MyConfig.class);
  12.         ((AbstractApplicationContext) ctx).stop();
  13.     }
  14. }

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<NotifyEvent>{

 

    @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执行结果

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值