Spring 事件机制
通过模拟邮件的发送,说明Spring的事件监听机制
事件类
package org.zln.module_chapter2.event; import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ApplicationContextEvent; /**
* 发送邮件事件类
* Created by sherry on 000005/7/5 22:10.
*/ public class MailSendEvent extends ApplicationContextEvent { private String to;
public MailSendEvent(ApplicationContext source,String to) {
super(source);
this.to = to;
} public String getTo(){
return this.to;
}
}
D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\src\org\zln\module_chapter2\event\MailSendEvent.java
事件监听类
package org.zln.module_chapter2.event; import org.springframework.context.ApplicationListener; /**
* 邮件发送 监听类
* Created by sherry on 000005/7/5 22:12.
*/ public class MailSendListener implements ApplicationListener<MailSendEvent> { /*对MailSendEvent进行处理*/
@Override
public void onApplicationEvent(MailSendEvent mailSendEvent) {
MailSendEvent mailSendEvent1 = mailSendEvent;
System.out.println("MailSendListener:向"+mailSendEvent1.getTo()+"发送一封邮件");
}
}
D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\src\org\zln\module_chapter2\event\MailSendListener.java
事件动作
package org.zln.module_chapter2.event; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; /**
* 邮件发送类
* Created by sherry on 000005/7/5 22:15.
*/
public class MailSender implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} public void sendMail(String to){
System.out.println("MailSender:模拟发送邮件");
MailSendEvent mailSendEvent = new MailSendEvent(applicationContext,to);
/*向容器中所有事件监听器发送事件*/
applicationContext.publishEvent(mailSendEvent);
}
}
D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\src\org\zln\module_chapter2\event\MailSender.java
事件注册
<!--事件-->
<bean class="org.zln.module_chapter2.event.MailSendListener"/>
<bean id="mailSender" class="org.zln.module_chapter2.event.MailSender"/>
测试
package org.zln.module_chapter2.event; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/org/zln/cfg/spring/applicationContext.xml"})/*启动Spring容器*/
public class MailSenderTest { @Autowired
@Qualifier("mailSender")
private MailSender mailSender; @Test
public void testSetApplicationContext() throws Exception { } @Test
public void testSendMail() throws Exception {
mailSender.sendMail("nbzlnzlq@126.com");
}
}
D:\GitHub\tools\JavaEEDevelop\Lesson17_JavaWebDemo\test\org\zln\module_chapter2\event\MailSenderTest.java
Spring 事件机制的更多相关文章
- Spring事件机制详解
一.前言 说来惭愧,对应Spring事件机制之前只知道实现 ApplicationListener 接口,就可以基于Spring自带的事件做一些事情(如ContextRefreshedEvent),但 ...
- 深入理解Spring事件机制(一):广播器与监听器的初始化
前言 Spring 从 3.x 开始支持事件机制.在 Spring 的事件机制中,我们可以令一个事件类继承 ApplicationEvent 类,然后将实现了 ApplicationListener ...
- spring事件机制
前置知识补充: 程序里面所谓的“上下文”就是程序的执行环境,打个比方:你就相当于web程序,你的房子就相当于web程序的上下文,你可以在家里放东西,也可以取东西,你的衣食住行都依赖这个房子,这个房子就 ...
- 搞清楚Spring事件机制后:Spring的源码看起来简单多了
本文主讲Spring的事件机制,意图说清楚: 什么是观察者模式? 自己实现事件驱动编程,对标Spring的事件机制 彻底搞懂Spring中的事件机制,从而让大家 本文内容较长,代码干货较多,建议收藏后 ...
- 观察者模式之spring事件机制
ddsspring中的事件机制使用到设计模式中的观察者模式 ,观察者模式有两个概念,1.观察者.被观察者.2.被观察者做出相应得动作,观察者能接收到.不分析设计模式,学习下spring中的事件机制实际 ...
- Spring的事件机制详解
同步事件和异步事件 同步事件:在一个线程里,按顺序执行业务,做完一件事再去做下一件事. 异步事件:在一个线程里,做一个事的同事,可以另起一个新的线程执行另一件事,这样两件事可以同时执行. 用一个例子来 ...
- Spring 中的事件机制
说到事件机制,可能脑海中最先浮现的就是日常使用的各种 listener,listener去监听事件源,如果被监听的事件有变化就会通知listener,从而针对变化做相应的动作.这些listener是怎 ...
- Spring 事件监听机制及原理分析
简介 在JAVA体系中,有支持实现事件监听机制,在Spring 中也专门提供了一套事件机制的接口,方便我们实现.比如我们可以实现当用户注册后,给他发送一封邮件告诉他注册成功的一些信息,比如用户订阅的主 ...
- Spring事件解析
首先介绍Spring事件相关类的关系: 其中EventListener与EventObject均是Java SE的范畴,源码如下: package java.util; public interfac ...
随机推荐
- WEB相关背景知识(新手)
1.评估域名类型 .com——商业实体 .edu——仅限有学位或更高等学历授予资格的高等教育使用 .gov——仅限政府使用 .net——与Internrt网络支持相关的团体,通常是Internet服务 ...
- XML 对xml文件的crud的增加 create操作 增加元素 增加属性
把创建的节点挂到上一节点的最后 找到参考节点,使用insertBefore方法进行插入位置 xml添加属性使用setAttribute方法
- java基础 final 修饰成员变量 只能赋值一次问题
final int a; public Fu(){ a=1; }
- CBCGPImage的GetSize的问题及解决方法
BCGControlBar Pro for MFC 25.10是目前(2018-07-16)网上能够找到的最新能够使用的版本,我配合Visual Studio 2010使用.在单文档MFC程序的视图中 ...
- (三)、python运算符和基本数据类型
运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 基本数据类型 1.数字 int(整形) # python3里不管数字有多长都叫整形# python2里分为整形和 ...
- MySQL创建民族表的SQL语句
MySQL创建民族表的SQL语句 CREATE TABLE `nation` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `nation` ) NOT NUL ...
- php-5.6.26源代码 - 扩展模块的种类,扩展模块的执行埋点
模块种类(两种) 类型一:zend的模块:(类似zend_extension=test.so) 识别方法: php.ini中以zend_extension开头的配置,如zend_extension=t ...
- My jdbc 错误
jdbc mysql插入数据提示Parameter index out of range (1 > number of parameters, which is 0). SqlStatement ...
- 数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图
二叉查找树(Binary Search Tree) 是一种树形的存储数据的结构 如图所示,它具有的特点是: 1.具有一个根节点 2.每个节点可能有0.1.2个分支 3.对于某个节点,他的左分支小于自身 ...
- python_字符串_常用处理
1. 输出原序列的反向互补序列 in1 = open("brca1.fasta", "r") out1 = open("re_brca1.fasta& ...