一、事件(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. mpi中的广播

    MPI可以实现一对多的集合通信,最常用的是广播:某个进程将数据广播到所有其他进程,最终的结果就是每个进程都有一份广播的数据.MPICH中的广播函数是MPI_Bcast(void* buffer,int ...

  2. SpriteBuilder修改CCB文件中的子CCB文件需要注意的一个地方

    在SpriteBuilder中如果一个CCB(比如一个场景)中嵌入了另一个子CCB文件(比如一个player),那么当给该子CCB中的root对象添加若干属性的时候,必须注意到这个并没有应用到父CCB ...

  3. java注解及在butternife中的实践和原理

    1.  背景 之前去一个公司,说到了java的注解,问java的注解有几种方式,然后我提到了android中的butternife和afinal注解工具,我们知道butternife在6.1版本的时候 ...

  4. ExtJS:GridPanel之renderer:function()和itemdblclick : function()方法参数详解

    要使用GridPanel,首先要定义Store,而在创建Store的时候必须要有Model,因此我们首先来定义Model: Ext.define("Gpsdata", { exte ...

  5. Socket层实现系列 — accept()的实现(二)

    本文主要分析accept()的阻塞等待和唤醒. 内核版本:3.6 Author:zhangskd @ csdn blog 等待队列 (1)socket的等待队列 /* * @sk_wq: sock w ...

  6. 【Android 应用开发】BluetoothSocket详解

    一. BluetoothSocket简介 1. 简介 客户端与服务端 : BluetoothSocket 和 BluetoothServerSocket 类似于Java中的套接字的 Socket 和 ...

  7. Gradle 1.12用户指南翻译——第四十二章. Announce插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  8. android 应用模式之mvp

    说到MVP就不得不提到MVC,做过J2EE的猿友们肯定知道MVC是个什么东西.MVC即 Model.View.Controller, 那MVP就Model.View.Presenter.Model用于 ...

  9. ThreadPoolExecutor运行机制

    最近发现几起对ThreadPoolExecutor的误用,其中包括自己,发现都是因为没有仔细看注释和内部运转机制,想当然的揣测参数导致,先看一下新建一个ThreadPoolExecutor的构建参数: ...

  10. 基于opencv的gpu与cpu对比程序,代码来自opencv的文档中

    原文链接: http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/gpu/gpu-basics-similarity/gpu-basi ...