10.1 Application Event

  • Spring使用Application Event给bean之间的消息通讯提供了手段
  • 应按照如下部分实现bean之间的消息通讯
    • 继承ApplicationEvent类实现自己的事件
    • 实现继承ApplicationListener接口实现监听事件
    • 使用ApplicationContext发布消息

10.2示例

示例中的通讯两个bean分别为DemoListener和Main

10.2.1 编写自定义的Application Event

package com.wisely.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;
} }

10.2.2 编写实现ApplicationListener的类

package com.wisely.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener implements ApplicationListener<DemoEvent> { public void onApplicationEvent(DemoEvent event) {
String msg = ((DemoEvent) event).getMsg();
System.out.println("我监听到了pulisher发布的message为:"+msg); } }

10.2.3 测试

package com.wisely.event;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Main { public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.event");
Main main =context.getBean(Main.class);
main.pulish(context);
context.close(); }
public void pulish(AnnotationConfigApplicationContext context){
context.publishEvent(new DemoEvent(this, "22"));
} }

输出结果

我监听到了pulisher发布的message为:22

10点睛Spring4.1-Application Event的更多相关文章

  1. Spring Application Event Example

    Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...

  2. Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)

    一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...

  3. SpringBoot -- 事件(Application Event)

    Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...

  4. spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持

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

  5. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  6. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  7. Spring 事件:Application Event

    Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...

  8. 从命令模式的维度理解Spring 之Application Event

    Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...

  9. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

随机推荐

  1. C++反汇编中的循环语句

    do while 效率是最高的 #include "pch.h" #include <iostream> int main() { ; ; do { nSum += n ...

  2. 检测 nginx 关闭切换keepalived

    检测nginx 端口启用  关闭 keepalived  检测 nginx 进程:然后关闭 keepalived ,关闭漂移IP : cat nginx_pid.sh #!/bin/bash whil ...

  3. mysql 函数表

    Name Description ABS() Return the absolute value ACOS() Return the arc cosine ADDDATE() Add time val ...

  4. P1903 [国家集训队]数颜色 (带修改莫队)

    题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...

  5. UOJ#221. 【NOI2016】循环之美 数论,杜教筛

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ221.html 题解 首先把题目转化为求 \[\sum_{x=1}^n \sum_{y=1}^m [\gcd(x,y) = ...

  6. [DOM] Input elements should have autocomplete attributes (suggested: autocomplete='tel', confirm at

    译文概要:输入元素应该有自动完成的属性,比如: autocomplete=’tel’. autocomplete 用途: 此功能主要是记住输入内容,下次提交表单或者浏览器回退后,还能保持表单内容不变. ...

  7. 【知识点】Java机密

    Java添加PDF图章.动态图章 主要实现以下功能: 添加图片图章.即通过加载现有的图章(以图片形式),添加到PDF指定页面位置 添加动态图章.即加载PDF文档,并在动态的添加印章内容,包括印章字样. ...

  8. element ui table组件自定义合计栏,后台给的数据

    合计的数据是后台传的,所以用table组件自定义一行用来合计 <el-table border fit v-loading.body="listLoading" elemen ...

  9. 7、vueJs基础知识07

    UI组件库 element-ui和mint-ui 其实都是借鉴了bootstrap bootstrap: 由twitter 开源 简洁.大方 官网文档https://www.bootcss.com/ ...

  10. ubuntu之路——day10.1 ML的整体策略——正交化

    orthogonalization 正交化的概念就是指,将你可以调整的参数设置在不同的正交的维度上,调整其中一个参数,不会或几乎不会影响其他维度上的参数变化,这样在机器学习项目中,可以让你更容易更快速 ...