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
*
*/
@Component
public class DemoEventListener implements ApplicationListener<DemoEvent>{ //使用注解@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
*
*/
@Component
public 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();
}
}

参考:http://blog.csdn.net/it_man/article/details/8440737

spring 事件(Application Event)的更多相关文章

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

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

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

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

  3. 精通SpringBoot--Spring事件 Application Event

    Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Be ...

  4. CL_GUI_ALV_GRID 触发PAI事件(Application event)

    *&---------------------------------------------------------------------* *& Report Z_BARRY_A ...

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

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

  6. Spring 事件:Application Event

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

  7. Spring Application Event Example

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

  8. SpringBoot -- 事件(Application Event)

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

  9. spring发布和接收定制的事件(spring事件传播)

    spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报  分类: 开源技术(如Struts/spring/Hibernat ...

随机推荐

  1. Oracle一个实例配置多个监听

    要想给一个Oracle实例配置多个监听,首先要定义多个监听器,因为是多个监听,势必会有一些监听端口不是1521. 现在服务端的listener.ora文件中定义如下监听器: LISTENER = (D ...

  2. Android PickerView滚动选择器的使用方法

    手机里设置闹钟需要选择时间,那个选择时间的控件就是滚动选择器,前几天用手机刷了MIUI,发现自带的那个时间选择器效果挺好看的,于是就自己仿写了一个,权当练手.先来看效果: 效果还行吧?实现思路就是自定 ...

  3. CentOS 配置本地yum源

    [root@localhost ~]#ls /media/dvd/                                                                   ...

  4. iOS - (base64对字符串加解密)

    今天公司让做支付系统,为了安全起见,需要对一些数据进行加密,然而我首想到的就是 base64 ,严格来说这不是一种加密方式,这只是将原有的一些字符串或者其它的一些文本进行一个转化而已,就是转化成数字, ...

  5. ios-控件的frame_center_bounds简单介绍

    frame 例如一个button按钮控件的frame frame是一个结构体,frame表示了button在它的父控件view中的位置---origin 以及---size origin也是一个结构体 ...

  6. fifo manage

    typedef struct { TEntry *header; TEntry *tailer; uint16 alloc; uint16 release;}TPoolCtrl;

  7. resin 64 & Java install

    [root@vohst voh]# uname -rnvohst 3.10.0-123.el7.x86_64 resin-pro-4.0.41.tar.gz Unrecognized option: ...

  8. ForkJoin框架

    1. 什么是Fork/Join框架 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 我们再通过 ...

  9. Python:操作文件

    python操作文件库不需要安装其他module,文件操作类库是python语言自身支持的操作. 判定文件是否存在:os.path.isfile(filePath) import os import ...

  10. 从零开始PHP攻略(3)——数据的存储与检索

    要点目录: I.保存数据 II.打开文件   III.创建并写入文件 IV.关闭文件 V.读文件 VI.给文件加锁 VII.删除文件 VIII.其他有用的文件操作函数 IX.数据库管理系统 1.保存数 ...