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. 从零開始学Swift之Hello World进化版

    上节课,也就是昨晚啦,我们学习到从零開始学Swift之Hello World.那一节仅仅有一句代码,大家会认为不够过瘾. 那么这节课,就给大家来多点瘾货吧! 先上图! //var 代表变量的类型, s ...

  2. 关于工作与生活——HP大中华区总裁孙振耀撰文谈退休并畅谈人生

    转自:http://blog.csdn.net/adaptiver/article/details/7494121 我有个有趣的观察,外企公司多的是25-35岁的白领, 40岁以上的员工很少,二三十岁 ...

  3. 2018,从AI看安卓生态的变革

    AI的发展与影响 与传统技术不同的是,AI技术算法清晰,优化目标明确,基础技术成熟,使得一众中小创企也看到了市场的机会.2017年中国企业动作频频,在自动驾驶,智能安防,智慧城市等领域都取得了不俗的成 ...

  4. Android控件ListView获取item中EditText值

    能够明白,如今没有直接方法能够获得ListView中每一行EditText的值. 解决方式:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值. 大概算法:重写Ba ...

  5. SetTimer时间间隔的问题

    1.用WM_TIMER来设置定时器   SetTimer函数的原型 UINT_PTR SetTimer( HWND hWnd,                      // 窗体句柄 UINT_PT ...

  6. Java并发基础:了解无锁CAS就从源码分析

    https://segmentfault.com/a/1190000015881923

  7. 一般处理程序页ashx 序列化 Json数组

    json传递数组到一般处理程序页,序列化为实体类的方法,可以解决.ENT framework 3.5以前的项目, 3.5以后的项目可以用System.ServiceModel.Web和System.R ...

  8. Entity Framework(1)——Connections and Models

    原文:https://msdn.microsoft.com/en-us/data/jj592674 应该选择CodeFirst.ModelFirst还是databaseFirst网上已经很多资料了,这 ...

  9. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  10. [Delphi]解决Delphi Distiller运行报错"HKEY_CURRENT_USER\\" is of wrong kind or size

    最近终于决心将使用多年的Delphi 7升级到Delphi 2007,虽然目前Delphi最高版本已经是XE8,但对于只做VCL开发的话还是喜欢2007这个经典的版本. 安装Delphi 2007一切 ...