一、事件(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)的更多相关文章

  1. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  2. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  3. Spring Boot实战系列(7)集成Consul配置中心

    本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...

  4. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

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

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

  6. JavaEE开发的颠覆者 Spring Boot实战--笔记

    1.Spring boot的三种启动模式 Spring 的问题 Spring boot的特点,没有特别的地方 1.Spring 基础 PS:关于spring配置 PS: 现在都已经使用 java配置, ...

  7. spring boot 自学笔记(四) Redis集成—Jedis

    上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例. 修改Red ...

  8. Spring Boot学习(四)常用注解

    一.注解对照表 注解 使用位置 作用  @Controller  类名上方  声明此类是一个SpringMVC Controller 对象,处理http请求  @RequestMapping  类或方 ...

  9. spring boot 实战笔记(一)

    spring 概述: Bean :每一个被 Spring 管理的 JAVA对象,都称之为 Bean.Spring提供一个IoC容器来初始化对象,负责创建Bean, 解决对象之间的依赖管理和对象的使用. ...

随机推荐

  1. shell的字符串和数字的转化(数字自动做字符串处理,变量名做字符串输出用单引号)

    shell里面怎么样把字符串转换为数字? 例如:a="024" 1,用${{a}} 2,用let达到(()) 运算效果. let num=0123; echo $num; 83 3 ...

  2. Java进阶(二十二)使用FileOutputStream写入文件

    Java使用FileOutputStream写入文件 绪 在Java中,文件输出流是一种用于处理原始二进制数据的字节流类.为了将数据写入到文件中,必须将数据转换为字节,并保存到文件.请参阅下面的完整的 ...

  3. imx51-linux的cpuinfo之分析

    这两天客户提出来,我们的平板cat /proc/cpuinfo出来的信息中的serial怎么是0. 客户就是上帝啊,没办法,分析找问题贝. 我们先看一下目前的cat /proc/cpuinfo的信息: ...

  4. Oracle Applications DBA 基础(二)

    6.OAM及系统管理 2014年9月13日 20:40 参考资料: 1.Oracle Applications System Administrator's Guide - Configuration ...

  5. mysql 好文章

    http://my.oschina.net/OpenSourceBO/blog/353464 http://www.oschina.net/p/cobar mysql集群 http://www.dew ...

  6. ViewPager切换动画PageTransformer的使用

    Android从3.0开始添加了属性动画后,诸多难以实现的动画都可以轻松解决了,v4包下的ViewPager控件当然也不例外,相对于非常平庸的默认切换动画,Google官方给我们展示了两个动画例子:D ...

  7. This version of the rendering library is more recent than your version of ADT plug-in. Please update

    预览layout.xml文件时提示: This version of the rendering library is more recent than your version of ADT plu ...

  8. C++项目中的extern "C" {}(转)

    注:本文转自吴秦先生的博客http://www.cnblogs.com/skynet/archive/2010/07/10/1774964.html#.吴秦先生的博客写的非常详细深刻容易理解,故特转载 ...

  9. Spring--ApplicationContext

    //中心接口,给应用提供配置信息 public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory ...

  10. Hbase问题

    Q: .meta.和root表是否要分裂? A: meta表和root表不会分裂,代码中有所判断. Q: 如果不分裂,那么都只有1个region? A: ... (查看代码后)A: meta和root ...