通行证│用户名: 密码: 验证码: 验证码,看不清楚?请点击刷新验证码 电信网通铁通移动   在线
文章搜索:
热门搜索:红客 黑鹰 红客技术 安全动画 红客培训
首页 文章 软件 动画 资源 励志 论坛 邮箱 会员 军事 科技 博客 爱心红客 最近更新 800g资源
 业内新闻 漏洞公告 病毒公告 电脑知识 网络知识 菜鸟入门 攻防教程 黑客攻防 安全编程 工具使用 综合安全 个人安全 安全相关 Q Q安全 原创精华 红客人物 站内事件
您现在的位置: 爱国者安全网 >> 文章类 >> 红客教程 >> 网络攻防 >> 文章正文
Spring中事件处理技巧
责任编辑:酷酷の鱼   更新日期:2008-4-13
 

来源:安全中国

Spring中提供一些Aware相关的接口,BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,其中最常用到的是ApplicationContextAware。实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例。ApplicationContextAware提供了publishEvent()方法,实现Observer(观察者)设计模式的事件传播机,提供了针对Bean的事件传播功能。通过Application.publishEvent方法,我们可以将事件通知系统内所有的ApplicationListener。 

  Spring事件处理一般过程:

  ·定义Event类,继承org.springframework.context.ApplicationEvent.

  ·编写发布事件类Publisher,实现org.springframework.context.ApplicationContextAware接口.

  ·覆盖方法setApplicationContext(ApplicationContext applicationContext)和发布方法publish(Object obj)

  ·定义时间监听类EventListener,实现ApplicationListener接口,实现方法onApplicationEvent(ApplicationEvent event).

  java 代码

import org.springframework.context.ApplicationEvent; 

/** 
* 定义事件信息 
* @author new 

*/ 
public class MessageEvent extends ApplicationEvent { 

 private String message; 

 public void setMessage(String message){ 
  this.message = message; 
 } 

 public String getMessage(){ 
  return message; 
 } 

 public MessageEvent(Object source, String message) { 
  super(source); 
  this.message = message; 
  // TODO Auto-generated constructor stub 
 } 

 private static final long serialVersionUID = 1L; 
}  

  java 代码

import org.springframework.beans.BeansException; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 
import org.springframework.context.support.FileSystemXmlApplicationContext; 

public class Publisher implements ApplicationContextAware { 

 private ApplicationContext context; 

 @Override 
 public void setApplicationContext(ApplicationContext arg0) 
 throws BeansException { 
  // TODO Auto-generated method stub 
  this.context = arg0; 
 } 

 public void publish(String message){ 
  context.publishEvent(new MessageEvent(this,message)); 
 } 

 public static void main(String[] args) { 
  ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml"); 
  Publisher pub = (Publisher) ctx.getBean("publisher"); 
  pub.publish("Hello World!"); 
  pub.publish("The quick brown fox jumped over the lazy dog"); 
 } 
}  

  java 代码

import org.springframework.context.ApplicationEvent; 
import org.springframework.context.ApplicationListener; 

public class MessageEventListener implements ApplicationListener { 

 @Override 
 public void onApplicationEvent(ApplicationEvent event) { 
  // TODO Auto-generated method stub 
  if(event instanceof MessageEvent){ 
   MessageEvent msEvent = (MessageEvent)event; 
   System.out.println("Received: " + msEvent.getMessage()); 
  } 
 } 
}  

  在运行期,ApplicationContext会自动在当前的所有Bean中寻找ApplicationListener接口的实现,并将其作为事件接收对象。当Application.publishEvent方法调用时,所有的ApplicationListener接口实现都会被激发,每个ApplicationListener可根据事件的类型判断是否是自己需要处理的事件,如上面的ActionListener只处理ActionEvent事件。

  • 上一篇文章:
  • 下一篇文章:
  • 最近更新
    推荐文章 瑞星公司04月13日发布 每日计算机病毒及木马播报
    普通文章 灰鸽子难被定义电脑病毒 八成杀毒软件无效
    普通文章 磁碟机与熊猫烧香 两大病毒对比
    普通文章 微软发布8个安全补丁 5个升级补丁
    普通文章 印报诬“中国黑客”入侵印外交部
    普通文章 USBKill8.0 实施保护的U盘病毒防火墙
    推荐文章 推荐:Windows 系统安全技术概念讲解
    普通文章 同时感染Win/Lin 跨平台病毒成趋势
    普通文章 程序从DOS/bios驻留内存到WINNT下监视读入内存数据
    普通文章 Windows 内核漏洞 ms08025 分析
    热门文章
    普通文章六步打造刀枪不入系统 让黑客无从下手
    普通文章教你六招 Linux 操作系统下的安全配置
    普通文章SQL 2005数据库 转到SQL 2000的步骤
    普通文章经典回顾十戒 认清防火墙评测与管理
    普通文章删除SQL Server 2000数据库危险扩展
    普通文章几招教你全面阻截 互联网电子邮件病毒
    推荐文章推荐:详细讲解ARP病毒发起欺骗攻击解决方法
    普通文章德国黑客组织威胁公布默克尔指纹
    普通文章李开复:电脑平均3个月左右重装一次系统
    普通文章金山07年营收5.566亿 毒霸增长132%
    精彩专题