博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring 事件(Application Event)
阅读量:7030 次
发布时间:2019-06-28

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

hot3.png

spring 事件(Application Event) 博客分类: java spring

spring 事件为bean 与 bean之间传递消息。一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件.

 

spring事件使用步骤如下:

1.先自定义事件:你的事件需要继承 ApplicationEvent

2.定义事件监听器: 需要实现 ApplicationListener

3.使用容器对事件进行发布

 

以下例子是场景是注册的时候发送邮件的一个场景:

先定义事件:

复制代码
package com.foreveross.service.weixin.test.springevent;import org.springframework.context.ApplicationEvent;/** * 自定义一个事件 * @author mingge * */public class DemoEvent extends ApplicationEvent{        private String msg;        private String email;    public DemoEvent(Object source,String msg,String email) {        super(source);        this.msg=msg;        this.email=email;    }    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    }
复制代码

然后定义事件监听器:

复制代码
package com.foreveross.service.weixin.test.springevent;import org.springframework.context.ApplicationListener;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Component;/** * 定义一个事件监听类 * @author mingge * */@Componentpublic class DemoEventListener implements ApplicationListener
{ //使用注解@Async支持 这样不仅可以支持通过调用,也支持异步调用,非常的灵活, @Async @Override public void onApplicationEvent(DemoEvent event) { System.out.println("注册成功,发送确认邮件为:" + event.getEmail()+",消息摘要为:"+event.getMsg()); }}
复制代码

再次使用容器对事件进行发布:

复制代码
package com.foreveross.service.weixin.test.springevent;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Component;/** * 事件发布类 * @author mingge * */@Componentpublic class DemoEventPublisher {        @Autowired    private ApplicationContext applicationContext;        public void pushlish(String msg,String mail){        applicationContext.publishEvent(new DemoEvent(this, msg,mail));    }    }
复制代码

最后就是测试了:

复制代码
package com.foreveross.service.weixin.test.springevent;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("com.foreveross.service.weixin.test.springevent")public class EventConfig {}
复制代码
复制代码
package com.foreveross.service.weixin.test.springevent;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * 事件测试类 * @author mingge * */public class EventTest {    public static void main(String[] args) {        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(EventConfig.class);        DemoEventPublisher demoEventPublisher=context.getBean(DemoEventPublisher.class);        demoEventPublisher.pushlish("张三1","565792147@qq.com");        demoEventPublisher.pushlish("张三2","565792147@qq.com");        demoEventPublisher.pushlish("张三3","565792147@qq.com");        demoEventPublisher.pushlish("张三4","565792147@qq.com");        demoEventPublisher.pushlish("张三5","565792147@qq.com");        context.close();    }}
复制代码

 

参考:

转载于:https://my.oschina.net/xiaominmin/blog/1597517

你可能感兴趣的文章
39.CSS3弹性伸缩布局【下】
查看>>
[javascript]图解+注释版 Ext.extend()
查看>>
我的前端工具集(七)div背景网格
查看>>
linux 下mongo 基础配置
查看>>
【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)...
查看>>
JUnit单元测试中的setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()方法小结
查看>>
java之jvm学习笔记六(实践写自己的安全管理器)
查看>>
Docker容器查看ip地址
查看>>
在PC端或移动端应用中接入商业QQ
查看>>
将python3.6软件的py文件打包成exe程序
查看>>
DataTable 排序
查看>>
大白话5分钟带你走进人工智能-第二十节逻辑回归和Softmax多分类问题(5)
查看>>
嵌入式系统在工业控制中的应用
查看>>
使用httpclient异步调用WebAPI接口
查看>>
c++ 类的对象与指针
查看>>
SSTI(模板注入)
查看>>
rbac models
查看>>
[2615]传纸条 sdutOJ
查看>>
类图标注的使用范例
查看>>
NumberFormat注解 DateTimeFormat
查看>>