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. iOS tableView高度缓存

    tableView计算完高度后,把高度缓存起来,避免下次重复计算,以减少不必要的消耗 // declare cellHeightsDictionary NSMutableDictionary *cel ...

  2. Java异常封装(自定义错误码和描写叙述,附源代码)

    真正工作了才发现.Java里面的异常在真正工作中使用还是十分普遍的. 什么时候该抛出什么异常,这个是必须知道的. 当然真正工作里面主动抛出的异常都是经过分装过的,自己能够定义错误码和异常描写叙述. 以 ...

  3. userdel命令

    userdel 功能:删除用户相关文件:/etc/passwd    /etc/shadow    /home/usernameuserdel [-r] username 常用参数:-r    连同家 ...

  4. lua 元表是个啥?

    function readOnly(t) local proxy = {} local mt = { __index = t, __newindex = function(t,k,v) error(& ...

  5. web 文件下载

    response.reset(); response.setContentType("octets/stream"); response.addHeader("Conte ...

  6. Js 抱错:::SyntaxError: identifier starts immediately after numeric literal

    SyntaxError: identifier starts immediately after numeric literal 今天写了个onclick()方法,有这样的一个变量4028b88161 ...

  7. wechat JS-SKD (getLoaction) 定位显示百度map

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. Bootstrap学习3--栅格系统

    备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html 目录1.简介2.栅格选项3.列偏移4.嵌套列5.列排序 ...

  9. Java实现时间日期格式转换示例

    package com.hanqi.util; import java.text.ParseException; import java.text.SimpleDateFormat; import j ...

  10. awk 字符串函数

    awk 提供了许多强大的字符串函数,见下表: awk 内置字符串函数 gsub(r,s) 在整个 $0 中用 s 替代 r gsub(r,s,t) 在整个 t 中用 s 替代 r index(s,t) ...