目录

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. import com.yangxiao.listener.config.MyConfig;
  6. public class ListenerTest {
  7. @Test
  8. public void testNotifyListener() {
  9. ApplicationContext ctx=new AnnotationConfigApplicationContext(MyConfig.class);
  10. ((AbstractApplicationContext) ctx).stop();
  11. }
  12. }

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

spring listener详尽篇相关推荐

  1. Spring第三篇【Core模块之对象依赖】

    tags: Spring 前言 在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博 ...

  2. java代码审计_Java代码审计| Spring框架思路篇

    Java的WEB框架是Java进阶课程,当要进行Spring的漏洞分析,要有一定的Java代码知识储备. Java后端标准的学习路线:JavaSE->JavaEE->Java Web框架 ...

  3. 第五篇:Spring源码篇-ApplicationContext

    Spring源码篇-ApplicationContext   前面通过手写IoC,DI.AOP和Bean的配置.到最后ApplicationContext的门面处理,对于Spring相关的核心概念应该 ...

  4. Spring MVC使用篇(八)—— 处理器(Handler)方法的返回值

    文章目录 1.演示项目环境搭建 1.1 演示项目工程结构 1.2 演示项目依赖的基础jar包 1.3 配置web.xml 1.4 配置Spring MVC核心配置文件 2.返回ModelAndView ...

  5. 玩转 Spring Boot 应用篇(搭建菜菜的店铺)

    0.  0.0. 历史文章整理 玩转 Spring Boot 入门篇 玩转 Spring Boot 集成篇(MySQL.Druid.HikariCP) 玩转 Spring Boot 集成篇(MyBat ...

  6. 白话Spring(基础篇)---AOP(execution表达式)

    [一知半解,就是给自己挖坑] 作为AOP的最后一节内容,我们来简单总结一下切面表达式上见的书写方法.下面的那内容有参考其他博文,在此先对开源博客的各位大神表示感谢! ----------------- ...

  7. Spring 事务原理篇:@EnableTransactionManagement注解底层原理分析技巧,就算你看不懂源码,也要学会这个技巧!

    前言 学习了关于Spring AOP原理以及事务的基础知识后,今天咱们来聊聊Spring在底层是如何操作事务的.如果阅读到此文章,并且对Spring AOP原理不太了解的话,建议先阅读下本人的这篇文章 ...

  8. Spring Cloud第九篇:链路追踪Sleuth

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Add sleuth to the classpath of a Spring Bo ...

  9. Spring Cloud第二篇:服务消费者RestTemplate+Ribbon

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

最新文章

  1. 内容推荐 | 生信技术与前沿内容知识库
  2. 【Java学习】从一个简单的HelloWorld项目中入门maven
  3. LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
  4. 无代码iVX编程实现简单跳跃超级玛丽游戏
  5. 【LeetCode笔记】958. 二叉树的完全性检验(Java、二叉树、BFS)
  6. Delphi TXLSReadWriteII导出Excel
  7. VMWare 8 安装 Mac OS 10.7 (Lion)版 【转】
  8. 人脸验证(四)--CenterLoss
  9. html5文本域禁止拖动,textarea用法 TextArea怎么禁用行滚动条
  10. 浅谈逆向——从案例谈OD的使用(OD的使用2)
  11. 保龄球计分c语言,保龄球的好处、起源、计分规则、常用技法
  12. 目标检测——Faster RCNN
  13. ACTS:首屈一指的软件测试策略是什么?
  14. 微服务Feign调用后开启Schedule报错No thread-bound request found: Are you referring to request attributes outsid
  15. C++中string类下的begin,end,rbegin,rend的用法
  16. 从RPA 向 IPA 转型升级!实在智能金秋发布会:重新定义流程自动化
  17. python tkinter button 透明图片_如何使Tkinter支持PNG透明?
  18. 【WEB】Web性能压力测试工具
  19. 2018年英语专升本英语阅读「Part II 阅读专区」【文章(图片)、答案、词汇记忆】
  20. AI时代,数据工程师必备知识总结

热门文章

  1. python输入float_float是什么意思_在python中 float是什么意思?
  2. 如何通过JavaScript发送http请求
  3. 计算机在地理科学中的作用,多媒体在地理教学中作用(精).doc
  4. 小妞会装机 -- 一个装机软件的开发笔记(八)
  5. Django文件部署(4.虚拟环境的配置)(全)
  6. 全国黄金价格数据(1978-2020年)
  7. 【Cocos2D-X 】初窥门径(8)判断精灵点击
  8. DP转HDMI后显示器无信号
  9. 在VSCode中使用LaTex,语法检测插件grammarly
  10. 什么样的公司可以申请高新技术企业?