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. Linux系统中缺少GUI设备引起的HeadlessException

    缺少X11显示设置 Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: No X11 DISPLA ...

  2. CentOS设置虚拟网卡做NAT方式和Bridge方式桥接

    CentOS设置虚拟网卡做NAT方式和Bridge方式桥接 http://www.centoscn.com/CentOS/config/2015/0225/4736.html 摘要:KVM虚拟机网络配 ...

  3. thinkphp 最简单的引入百度编辑器的方法

    后台肯定是框架了...编辑器放在框架里面... 后台首页比如是这样的... <html> <frameset cols="10%,90%"> <fra ...

  4. 一些Demo链接

    youtube下载神器:https://github.com/rg3/youtube-dl我擦咧vim插件:https://github.com/Valloric/YouCompleteMevim插件 ...

  5. Android --Fragment中异步HTTP请求

    1.自定义异步Task /** * 异步获取实时数据 */ public class TimerDataTask extends AsyncTask<Void, Void, Boolean> ...

  6. javascript:void(0)和javascript:;的用法

    一.JavaScript:void(0) 我们经常会使用到 javascript:void(0) 这样的代码,那么在 JavaScript 中 javascript:void(0) 代表的是什么意思呢 ...

  7. 第六篇 SQL Server安全执行上下文和代码签名

    本篇文章是SQL Server安全系列的第六篇,详细内容请参考原文. SQL Server决定主体是否有必要的执行代码权限的根本途径是其执行上下文规则.这一切都可能复杂一个主体有执行代码的权限,但是却 ...

  8. 终于发现为什么SQL没有释放句柄,原来是保存句柄的变量被覆盖了,丢失了原来的句柄

    stmt = xxx ,  stmt = yyy ,  stmt.close() 之前的xxx 没有close掉

  9. HTML是什么

    HTML(Hyper Text Mark-up Language )即超文本标记语言,是 WWW 的描述语言,由 Tim Berners-lee提出.设计 HTML 语言的目的是为了能把存放在一台电脑 ...

  10. openSuSE DNS SERVER CONFIG

    system:openSuSE 12.3(much better and frendly than the 12.1 in network config)1,network config,attent ...