什么是ApplicationContext?
它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。
ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。
ApplicationEvent
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
ApplicationListener
是一个接口,里面只有一个onApplicationEvent方法。
所以自己的类在实现该接口的时候,要实装该方法。
如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Observer设计模式。
首先创建一个Event事件类:
- 1. public class EmailListEvent extends ApplicationEvent {
- 2.
- 3. private static final long serialVersionUID = 1L;
- 4. public String address;
- 5. public String text;
- 6.
- 7. public EmailListEvent(Object source) {
- 8. super(source);
- 9. }
- 10.
- 11. public EmailListEvent(Object source, String address, String text) {
- 12. super(source);
- 13. this.address = address;
- 14. this.text = text;
- 15. }
- 16.
- 17. public void print() {
- 18. System.out.println("Hello,Spring Event!!!");
- 19. }
- 20. }
其次创建一个ApplicationListener类:
- 1. public class EmailNotifier implements ApplicationListener {
- 2.
- 3. public void onApplicationEvent(ApplicationEvent evt) {
- 4. if (evt instanceof EmailListEvent) {
- 5. EmailListEvent emailEvent = (EmailListEvent) evt;
- 6. emailEvent.print();
- 7. System.out.println("the source is:" + emailEvent.getSource());
- 8. System.out.println("the address is:" + emailEvent.address);
- 9. System.out.println("the mail's context is :" + emailEvent.text);
- 10. }
- 11.
- 12. }
- 13. }
接着将Listener注册到Spring的xml文件中:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <bean id="emailListListener" class="spring.event.EmailNotifier"></bean>
- </beans>
最后创建Demo类:
- 1. public class ListenerEventDemo {
- 2.
- 3. /**
- 4. * @param args
- 5. */
- 6. public static void main(String[] args) {
- 7. ApplicationContext context = new ClassPathXmlApplicationContext(
- 8. "SpringEvent.xml");
- 9. EmailListEvent emailListEvent = new EmailListEvent("hello",
- 10. "helloSpring@sina.com", "this is a test eamil content");
- 11. //在ApplicationContext中发布一个 ApplicationEvent
- 12. context.publishEvent(emailListEvent);
- 13. }
- 14.
- 15. }
测试结果:
- # Hello,Spring Event!!!
- # the source is:hello
- # the address is:helloSpring@sina.com
- # the mail's context is :this is a test eamil content
相关推荐
Spring使用ApplicationEvent和ApplicationListener两个核心接口实现了事件监听机制。开发者可以自定义事件类和事件监听器,并通过应用程序上下文的publishEvent方法来发布事件,Spring框架会自动分发事件给所有对该...
主要介绍了Spring ApplicationListener监听器用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Spring ApplicationListener的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
TODO Spring注解驱动开发第39讲——你不知道的ApplicationListener的原理
TODO Spring注解驱动开发第38讲——你知道ApplicationListener的用法吗?
Spring注入Filter与Listener的方法.png
Spring的ApplicationEvent事件和监听器的测试工程,演示了如何使用Spring中的事件和监听器内容。完整的maven工程,能够运行。
主要介绍了SpringMVC事件监听ApplicationListener实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1. 发布订阅/观察者模型,实现业务之间的解耦。 2.比如新增用户完成需要发短信、记录日志等,这些逻辑堆在新增方法的最后面实在不太合理,而且不易复用。 3.对解耦代码有很大的帮助,比如多种业务场景做完都要插入...
内容:中介者模式、观察者模式/监听者模式实现,用来消息发送和消息处理的解耦,以单播和多播形式同步或异步的模式发布消息,...能学到:ApplicationContext、ApplicationEvent、ApplicationListener,@EventListener
这种机制基于观察者模式,由事件(ApplicationEvent)、事件监听器(ApplicationListener)和事件广播器(ApplicationEventMulticaster)三个主要组成部分构成。 事件是发生的动作或事情,其核心是ApplicationEvent...
基于Spring的listener和Task,Spring的初始化和定时任务(Task)
ApplicationEvent 是 Spring 框架中的一个重要概念,它是...这个接口定义了一个方法:void onApplicationEvent(E event),当某个 ApplicationEvent 被发布时,所有注册监听该事件的 ApplicationListener 将会被调用。
Spring卡夫卡听众样本如果您的侦听器引发异常,则使用ErrorHandler进行有关Spring Kafka侦听器的示例项目。技术领域Sprint Boot春天卡夫卡环境要求该示例已在本地安装中使用Apache Kafka 0.10.2进行了测试。Apache ...
spring-cloud 2.0 集成工作流 flowable
11. Developing Your First Spring Boot Application 11.1. Creating the POM 11.2. Adding Classpath Dependencies 11.3. Writing the Code 11.3.1. The @RestController and @RequestMapping Annotations 11.3.2. ...
spring-kafka顺序消费。 kafka顺序消费。 主要是为博客上传完整代码
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 1.创建一个类实现 ServletContextListener (具体实现哪个Listener根据情况来判断) 2.在类上加入注解 @WebListener 3.重写 context...
监听器(Listener)的注册方法和 Servlet 一样,有两种方式:代码注册或者注解注册。接下来通过本文给大家介绍Spring Boot的listener(监听器)简单使用,需要的朋友可以参考下
给大家介绍了spring-boot中如何定义过滤器、监听器和拦截器,对Spring Boot 编写Servlet、Filter、Listener、Interceptor的相关知识感兴趣的朋友一起看看吧