博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1. Spring 4.2.3前瞻-更简单的Application Event
阅读量:4118 次
发布时间:2019-05-25

本文共 2226 字,大约阅读时间需要 7 分钟。

1.1 Application Event

  • Spring 4.1的写法请参考

  • 请对比

  • 使用一个@EventListener取代了实现ApplicationListener接口,使耦合度降低;

1.2 示例

  • 包依赖
4.0.0
com.wisely
spring4-2
0.0.1-SNAPSHOT
jar
spring4-2-3
http://maven.apache.org
UTF-8
org.springframework
spring-context
4.2.3.RC1
spring-milestones
Spring Milestones
http://repo.spring.io/milestone
false
  • 编写自定义的Application Event
  • package com.wisely.spring4_2.3.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;    }}
  • 编写监听类
  • package com.wisely.spring4_2.3.event;import org.springframework.context.event.EventListener;import org.springframework.stereotype.Component;@Componentpublic class DemoListener {    @EventListener //注意此处    public void handleDemoEvent(DemoEvent event){        System.out.println("我监听到了pulisher发布的message为:"+event.getMsg());    }}
  • 测试
  • package com.wisely.spring4_2.3.event;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class Main {    public static void main(String[] args) {        AnnotationConfigApplicationContext context =                   new AnnotationConfigApplicationContext("com.wisely.spring4_2.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

转载地址:http://qjcpi.baihongyu.com/

你可能感兴趣的文章
Objective-C 基础入门(一)
查看>>
Flutter Boost的router管理
查看>>
iOS开发支付集成之微信支付
查看>>
C++模板
查看>>
【C#】如何实现一个迭代器
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
DirectX11 光照演示示例Demo
查看>>
VUe+webpack构建单页router应用(一)
查看>>
Node.js-模块和包
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
PHP 7 的五大新特性
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
OpenCV meanshift目标跟踪总结
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
听说玩这些游戏能提升编程能力?
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>