spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持
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之间通信提供了支持的更多相关文章
- spring boot 配置注入
spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...
- Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Spring Boot动态注入删除bean
Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...
- Spring boot Value注入 未整理 待完善
Springboot 热部署Springboot为开发者提供了一个名叫 spring-boot-devtools来使Springboot应用支持热部署,提供开发者的开发效率,无需手动重启Spring ...
- Spring boot 的 properties 属性值配置 application.properties 与 自定义properties
配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...
- 笔记65 Spring Boot快速入门(五)
SpringBoot+JPA 一.什么是JPA? JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期 ...
- spring boot servlet 注入
spring boot 注入servlet的方法是借助ServletRegistrationBean这个类 例子如下: 先建一个servlet import java.io.IOException; ...
- Spring Boot 2.x(五):整合Mybatis-Plus
简介 Mybatis-Plus是在Mybatis的基础上,国人开发的一款持久层框架. 并且荣获了2018年度开源中国最受欢迎的中国软件TOP5 同样以简化开发为宗旨的Spring Boot与Mybat ...
随机推荐
- 《HBase in Action》 第二章节的学习总结 ---- HBase基本组成
准备工作:采用的HBase版本是:CDH4.5,其中的Hadoop版本是:hadoop-2.0.0-cdh4.5.0:HBase版本是:hbase-0.94.6-cdh4.5.0: Hbase的配置文 ...
- KSTORE日常工作遇到问题总结
1.csv导入kstore命令语句 oimpexp -F "E:/127.csv" -S KSTORE -T "T_BUSDATA" -d 1 -z -B 7 ...
- Linux tar包安装Nginx
1.首先安装依赖包(依赖包有点多.我们採用yum的方式来安装) yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ...
- XSD文件详解(二)
<?xml version="1.0" encoding="gb2312"?> <studentlist> <student ...
- 鸟哥的Linux私房菜-----6、文件与文件夹管理
- Android 与H5之间的js交互
之前项目做过一些Android和Html5之间js交互方面的东西,今天有时间就总结一下: 一.为什么要进行js交互: 为了方便原生开发和Html之间数据传递,在静态页面的情况下可以改变原生开发的页面: ...
- php修改密码
为了让页面更为好看一些,我一般会选择bootstrap,写起来虽然看着麻烦,但是我们真正需要的只有中间的内容 下面是html的内容 <div id="tbx"" ...
- Ubuntu 12.04使用uginx+fastcgi-mono-server2部署asp.net 网站
Ubuntu 12.04使用uginx+fastcgi-mono-server2部署asp.net 网站 1.安装nginx和mono-fastcgi-server2 sodu apt-get in ...
- 合理的布局,绚丽的样式,谈谈Winform程序的界面设计
转载,不错的学习文章 阅读后,起初不太明白,试验了几次后明白了dev的强大.从事Winform开发很多年了,由于项目的需要,设计过各种各样的界面效果.一般来说,运用传统的界面控件元素,合理设计布局,能 ...
- linux c编程:信号(二) alarm和pause函数
使用alarm函数可以设置一个定时器,在将来的某个时刻该定时器超时.当定时器超时后,产生SIGALRM信号.如果忽略或不捕捉此信号,则其默认动作是终止调用该alarm函数的进程 #include< ...