Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)
一、事件(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 {
@Autowired
ApplicationContext 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();
}
}
运行结果:

Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)的更多相关文章
- Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档
0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...
- spring boot实战(第十三篇)自动配置原理分析
前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...
- Spring Boot实战系列(7)集成Consul配置中心
本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...
- Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...
- Spring Boot实战笔记(一)-- Spring简介
一.Spring 概述 Spring框架是一个轻量级的企业级开发的一站式解决方案.所谓的解决方案就是可以基于Spring解决所有的Java EE开发的所有问题. Spring框架主要提供了Ioc(In ...
- JavaEE开发的颠覆者 Spring Boot实战--笔记
1.Spring boot的三种启动模式 Spring 的问题 Spring boot的特点,没有特别的地方 1.Spring 基础 PS:关于spring配置 PS: 现在都已经使用 java配置, ...
- spring boot 自学笔记(四) Redis集成—Jedis
上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例. 修改Red ...
- Spring Boot学习(四)常用注解
一.注解对照表 注解 使用位置 作用 @Controller 类名上方 声明此类是一个SpringMVC Controller 对象,处理http请求 @RequestMapping 类或方 ...
- spring boot 实战笔记(一)
spring 概述: Bean :每一个被 Spring 管理的 JAVA对象,都称之为 Bean.Spring提供一个IoC容器来初始化对象,负责创建Bean, 解决对象之间的依赖管理和对象的使用. ...
随机推荐
- myeclipse 彻底让烦人的各种验证消失 让你的开发速度飞快
大家都知道,myeclipse的验证很吭爹,不但保存的时候要难,BUILD的时候也要验.常常为了等它而浪费了大浪的时间!!真不知道设计人员当初是怎么加进这种功能的.真心不需要. 以前都是到window ...
- apache tomcat集群
今天花了大概两个小时完成了tomcat与apache的集群.现记录一下,也希望能帮助后来者. 建议看这篇博客前,先阅读一下鄙人拙作 tomcat整合apache 看完那个后,再进行集群,就很快了. 和 ...
- 网站开发进阶(二十六)js刷新页面方法大全
js刷新页面方法大全 在项目开发过程中,需要实现刷新页面.经过学习,发现下面这条语句就可以轻松实现. location.reload(); // 刷新页面 有关刷新页面的其它方法,具体学习内容如下,有 ...
- 11_Eclipse中演示Git版本的创建,历史版本的修改,创建分支,合并历史版本和当前版本
1 执行以下案例: 某研发团队2011年初开发了一款名为Apollo的信息系统,目前已发布v1.0版本.此项目初期已有部分基础代码, 研发团队再此基础代码上经过3个月的努力发布了一个功能相对完备 ...
- Java进阶(十四)实现每天定时对数据库的操作
Java实现每天定时对数据库操作 现在有一个很棘手的问题:客户要求实现一个功能,就是每日凌晨自动计算慢性病订单是否有需要在今日提醒的,如果有则生成一条提醒记录到lm_notice之中. 如何在Web工 ...
- Linux进程管理(第二版) --计划任务
计划任务 一.一次性计划任务 月11日) at 5:30pm at 17:30 [today] #today可省略 at now + 3 hours at now + 180 minutes at 1 ...
- iOS8 UILocalNotification 增加启动授权
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46810357 ...
- 第一个Polymer应用 - (0)准备工作
原文链接: Getting Started - Your first Polymer application翻译时间: 2014年7月5日翻译人员: 铁锚 关于Polymer 的简介,请参考 CSD ...
- Linux下进程通信方式(简要概述)
http://blog.sina.com.cn/s/blog_65c209580100u0ee.html (1)管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖先 ...
- 网站系统压力测试Jmeter+Badboy
最近项目需要压力测试,因此搜了几款试用,首选的是LoadRunner这款大名鼎鼎的测试软件: LoadRunner11 下载请猛戳这里 传送门LoadRunner破解文件 下载请猛戳这里 传送门Loa ...