一、事件(Application Event)

  Spring的事件为Bean和Bean之间的消息通信提供了支持。当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要让另一个Bean监听当前Bean所发送的事情。

  Spring的事件需要遵循如下流程:

  (1)自定义事件,集成ApplicationEvent。

  (2)定义事件监听器,实现ApplicationListener。

  (3)使用容器发布事件。

示例:

  1.自定义事件。

package com.ecworking.event;import org.springframework.context.ApplicationEvent;public class DemoEvent extends ApplicationEvent{private static final long serialVersionUID = 1L;private String msg;public DemoEvent(Object source, String msg) {super(source);this.msg = msg;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}
}

  2.事件监听器。

package com.ecworking.event;import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;@Component
public class DemoListener implements ApplicationListener<DemoEvent> { //实现ApplicationListener接口,并指定监听的事件类型。public void onApplicationEvent(DemoEvent event){ //使用onApplicationEvent方法对消息进行接受处理。String msg = event.getMsg();System.out.println("bean-demoListener 接收到了 bean-demoPublisher 发布的消息:" + msg);}
}

  3.事件发布类。

package com.ecworking.event;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;@Component
public class DemoPublisher {@AutowiredApplicationContext applicationContext; //注入ApplicationContext用来发布事件。public void publish(String msg){applicationContext.publishEvent(new DemoEvent(this, msg)); //使用 ApplcationContext 的 publishEvent 方法来发布。
    }
}

  4.配置类。

package com.ecworking.event;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan("com.ecworking.event")
public class EventConfig {
}

  5.运行。

package com.ecworking.event;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);demoPublisher.publish("hello application event");context.close();}
}

运行结果:

转载于:https://www.cnblogs.com/dyppp/p/7581349.html

Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)相关推荐

  1. spring boot实战(第四篇)分散配置

    前言 分散配置是系统必不可少的一部分,将配置参数抽离出来为后期维护提供很大的便利.spring boot 默认支持两个格式的配置文件:.properties .yml. .properties与.ym ...

  2. Spring Boot学习笔记:Spring Boot核心配置

    文章目录 一.Spring Boot基本配置 (一)入口类与@SpringBootApplication 1.项目入口类 - SpringBootDemoApplication 2.核心注解 - @S ...

  3. Spring Boot实战笔记(一)-- Spring简介

    一.Spring 概述 Spring框架是一个轻量级的企业级开发的一站式解决方案.所谓的解决方案就是可以基于Spring解决所有的Java EE开发的所有问题. Spring框架主要提供了Ioc(In ...

  4. Spring Boot学习笔记:Spring Boot的Web功能

    文章目录 一.Spring Boot的Web支持 二.Thymeleaf模板引擎 (一)Thymeleaf基础知识 1.引入Thymeleaf 2.访问Model数据 3.Model中的数据迭代 4. ...

  5. 程序猿最终之路-架构师:Spring boot实战战役解析

    什么是架构师 所谓架构师,通俗地说就是设计师或结构设计者,这些定义如果用在建筑学上,则是很容易理解的.在软件工程领域中,软件架构师实际上就是软件项目的总体设计师,是软件组织新产品的开发与集成.新技术体 ...

  6. springboot thymeleaf配置_【程序源代码】Spring Boot 开发笔记web开发实战1

    关键字:<Spring Boot 开发笔记>系列文章 各位亲爱的小伙伴:大家好! <Spring Boot 开发笔记>系列文章 这套笔记和源码是我自己在学习springboot ...

  7. 【Spring Boot实战】源码解析Spring Boot自动配置原理

    一.简介 Spring致力于让Java开发更简单,SpringBoot致力于让使用Spring进行Java开发更简单,SpringCloud致力于基于SpringBoot构建微服务生态圈,让微服务开发 ...

  8. 《Java EE 开发的颠覆者:Spring Boot实战》读书笔记

    第一章 Spring基础 Spring发展阶段 xml配置 注解配置(应用的基本配置,如数据库配置采用xml文件,业务配置有注解) java配置 每一个被Spring管理的java对象都称之为bean ...

  9. ssm如何支持热部署_最新Spring Boot实战文档推荐:项目搭建+配置+SSM整合

    在Spring Boot项目中,正常来说是不存在XML配置,这是因为Spring Boot不推荐使用XML,注意,排不支持,Spring Boot推荐开发者使用Java配置来搭建框架, Spring ...

  10. springboot做系统所需的软硬件环境_最新Spring Boot实战文档推荐:项目搭建+配置+SSM整合...

    在Spring Boot项目中,正常来说是不存在XML配置,这是因为Spring Boot不推荐使用XML,注意,排不支持,Spring Boot推荐开发者使用Java配置来搭建框架, Spring ...

最新文章

  1. Robotium todolist.test.elements
  2. python中的一些算法
  3. Fashion-MNIST下载地址
  4. PS里建立工作路径对话框中的“容差”是干什么的?
  5. 站酷用HTML5播放视频,站酷:动效展示实践的问题及解决
  6. powerbi视觉对象_Power BI 视觉对象词云WordCloud
  7. CentOS4.4下安装jdk1.5
  8. 用两种方法求解九宫算问题
  9. 接口可以继承多个接口总结
  10. JAVA中整型常量的长度,Java基础入门篇(三)——Java常量、变量,
  11. Maven的核心笔记(3)常用命令、坐标和仓库
  12. 掌握Android阅读源码的两种简单方式
  13. python加粗字体_Python:openpyxl将字体更改为粗体 - python
  14. 分区表修复工具--DISKFIX
  15. 基于华为云IoT设计的智能门锁
  16. java计算机毕业设计销售人员绩效管理系统源码+系统+数据库+lw文档(1)
  17. C语言编写贪吃蛇游戏
  18. newifi路由器 php,新路由newifi固件下载
  19. BIOS 虚拟化技术
  20. 微信支付商户平台,企业付款,企业向个人付款接口总结

热门文章

  1. oracle报错ORA-01843: not a valid month
  2. Flutter使用简报
  3. 面向对象编程设计练习题(2)
  4. Apax Partners收购ThoughtWorks幕后探秘
  5. java.text.MessageFormat 专题
  6. 腾讯2014在广州站实习生offer经验(TEG-开发背景)
  7. MySQL如何建索引以及利用索引优化ORDER BY排序语句
  8. 光纤测试、OTDR使用经验、FS530OTDR使用经验
  9. “无法保存该网页”的最简单解决方法
  10. [生活日记] 社会真的这么乱?尼姑也行骗,道德何在