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

最新文章

  1. spring context上下文(应用上下文webApplicationContext)(转载)
  2. 解决虚拟机中使用ntpdate报错:ntpdate[46700]: no server suitable for synchronization found
  3. @UniqueConstraint
  4. Python:利用内建函数将字符串转化为整数
  5. webservice jsonp格式调用
  6. [译]Mongoose指南 - 查询
  7. 7z命令行工具
  8. 转 BHO API HOOK Wininet基于IE编程的一些资料
  9. JS对json对象的调用成员2种方式
  10. EasyUI 兼容 IE6 方法总结
  11. SqlServer 笔记
  12. mysql简单建表
  13. JavaScript函数使用和DOM节点
  14. 关于eclipse运行TestNG出现: CreateProcess error=206, &#206;ļ&#254;&#195;&#251;&#187;&#242;)չ&#195;&#251;的解决办法
  15. Javascript 将字符串替换为特定的规律的字符串
  16. Python 爬虫实例(爬百度百科词条)
  17. CentOS 6.5环境下heartbeat高可用集群的实现及工作原理详解
  18. xcode工程编译错误:一般错误总结
  19. react复习总结(1)--react组件开发基础
  20. 『转』android官网翻译好的蓝牙API接口说明

热门文章

  1. iOS开发中(null)与&lt;null&gt;的判断
  2. DBCC TRACEON/TRACEOFF/TRACESTATUS
  3. iOS:runtime最全的知识总结
  4. iOS:app直播---原理篇
  5. QMainWindow的setLayout的问题
  6. 微信接口请求万能函数http_request
  7. mkdir
  8. error LNK2005 int __cdecl 解决方案【转】
  9. applicationContext.xml文件放置位置不同而导致的jUnit测试的时候路径的不同
  10. MVC部署 - 错误集锦