spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件。

spring的事件应该遵循:

1.自定义事件,集成:ApplicationEvent

2.自定义事件监听,实现ApplicationListener

3.使用容器发布事件

//自定义事件

package ch2.event;
import org.springframework.context.ApplicationEvent; //自定义事件 public class DemoEvent extends ApplicationEvent { private static final long serialVerisionUID = 1L;
private String msg; public DemoEvent(Object source, String msg) {
super(source);
// TODO Auto-generated constructor stub
this.msg = msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }

  

//事件监听器

package ch2.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; //事件监听器 //@Component把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>
//@service注入:当前类是spring管理的一个bean
@Component
public class DemoListener implements ApplicationListener<DemoEvent> { @Override
public void onApplicationEvent(DemoEvent event) {
// TODO Auto-generated method stub //接受消息
String msg = event.getMsg();
//打印消息
System.out.println("我(bean-DemoListener)接收到了bean-DemoPublisher发布的消息:"+msg); } }

  

//事件发送

package ch2.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; //事件发送 //把普通pojo实例化到spring容器中当成一个Bean
@Component
public class DemoPublisher { //将ApplicationContext类的实体Bean注入到DemoPublisher中,让DemoPublisher拥有ApplicationContext的功能
//使用applicationContext来发布事件
@Autowired
ApplicationContext applicationContext; public void publish(String msg)
{
//使用applicationContext的event来发布消息
applicationContext.publishEvent(new DemoEvent(this, msg));
} }

  

配置类

package ch2.event;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan; //声明本类是一个配置类
@Configuration
//导入ch2.event包下的所有@Service,@Component,@Repository,@Controller注册为Bean
@ComponentScan("ch2.event")
public class EventConfig { }

  

运行:

package ch2.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();
} }

  

结果:

我(bean-DemoListener)接收到了bean-DemoPublisher发布的消息:hello application event

spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持的更多相关文章

  1. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

  2. Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  3. Spring Boot动态注入删除bean

    Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...

  4. 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案

    搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...

  5. Spring boot Value注入 未整理 待完善

    Springboot 热部署Springboot为开发者提供了一个名叫 spring-boot-devtools来使Springboot应用支持热部署,提供开发者的开发效率,无需手动重启Spring ...

  6. Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

    配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...

  7. 笔记65 Spring Boot快速入门(五)

    SpringBoot+JPA 一.什么是JPA? JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期 ...

  8. spring boot servlet 注入

    spring boot 注入servlet的方法是借助ServletRegistrationBean这个类 例子如下: 先建一个servlet import java.io.IOException; ...

  9. Spring Boot 2.x(五):整合Mybatis-Plus

    简介 Mybatis-Plus是在Mybatis的基础上,国人开发的一款持久层框架. 并且荣获了2018年度开源中国最受欢迎的中国软件TOP5 同样以简化开发为宗旨的Spring Boot与Mybat ...

随机推荐

  1. 关于ViewData,ViewBag,TempData三者学习记录!

    关于ViewData,ViewBag,TempData三者学习分享! 1.ViewData和TempData是字典类型,赋值方式用字典方式,ViewData["Key"] . 2. ...

  2. Visual Studio的 Apache Cordova 插件CTP3.0发布!

    北京时间12号晚23点开始的Connect()活动上,微软发布了一系列激动人心的消息! .NET开源了!以后.NET将可在Linux和Mac OS平台上运行! VS免费了!!如果你是学生,个人开发者, ...

  3. PlSql加入数据库链接

    Oracle-OraDb10g_home2  ->  配置和移植工具 -> net Manager -> 本地 -> 服务命名 -> 加入(网络服务名自己命名,主机名:要 ...

  4. MIC中的数据传输

    先看一段代码,如下 #include<stdlib.h> #include<stdio.h> #define LEN 5 int main(int argc,char** ar ...

  5. jquery简洁遮罩插件

    /************************** *Desc:提交操作时遮罩 *Argument:type=0 全屏遮 1局部遮 *Author:Zery-Zhang *Date:2014-09 ...

  6. UFLDL深度学习笔记 (七)拓扑稀疏编码与矩阵化

    UFLDL深度学习笔记 (七)拓扑稀疏编码与矩阵化 主要思路 前面几篇所讲的都是围绕神经网络展开的,一个标志就是激活函数非线性:在前人的研究中,也存在线性激活函数的稀疏编码,该方法试图直接学习数据的特 ...

  7. ios NavigationViewController跳转以及返回传值

    (一)使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面.这种话.下一页面相同在NavigationViewContr ...

  8. Unity 插件收集(持续更新)

    MGS Machinery Unity绑定机械关节,铰链,机构插件包.    MGS Mechanical Drive 用于绑定场景中的机械驱动器的Unity插件   Unity Wave Propa ...

  9. Frobenius inner product

    https://en.wikipedia.org/wiki/Frobenius_inner_product Frobenius norm

  10. 什么是GIL锁以及作用

    全局解释锁,每次只能一个线程获得cpu的使用权:为了线程安全,也就是为了解决多线程之间的数据完整性和状态同步而加的锁,因为我们知道线程之间的数据是共享的.