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. http://www.cnblogs.com/littlemonk/p/5705476.html

    http://www.cnblogs.com/littlemonk/p/5705476.html

  2. ionic一些常见问题及方法

    1.打包的app无法访问互联网及网络资源(比如网页调试可以请求数据,安装到手机上无法请求数据) 添加白名单插件 ionic plugin add https://github.com/apache/c ...

  3. 端口转发后执行putty连接------------------》VirtualBox+ubuntu_server

    login as: fleam fleam@127.0.0.1's password: Welcome to Ubuntu LTS (GNU/Linux --generic i686) * Docum ...

  4. 分布式消息系统Kafka初步

    终于可以写kafka的文章了,Mina的相关文章我已经做了索引,在我的博客中置顶了,大家可以方便的找到.从这一篇开始分布式消息系统的入门. 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到 ...

  5. JRE和JDK的区别

    1. 定义 JRE(Java Runtime Enviroment)是Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程序.JRE是 ...

  6. mysql修改表名,列名,列类型,添加表列,删除表列

    alter table test rename test1; --修改表名 ); --添加表列 alter table test drop column name; --删除表列 ) --修改表列类型 ...

  7. .NET中常见对象类型

    .NET中六大内置对象:1.Response    2.Request   3.Session   4.Appliction  5.Server  6.Cookie System.Web.HttpCo ...

  8. ASP.NET MVC Error

    Error Handler http://prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc http://ww ...

  9. weka特征选择(IG、chi-square)

    一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...

  10. leetcode143- Reorder List问题

    题目要求: Given a singly linked list L: L0→L1→…→Ln-1→Ln,    reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You m ...