Spring中ApplicationContext对事件的支持

本文介绍了Spring框架中事件发布和监听的基本原理与实现方法。通过自定义事件UserEvent及监听器UserListener,演示了如何在Spring中发布和接收事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Spring



ApplicationContext

对事件的支持



ApplicationContext

具有发布事件的能力。

这是因为该接口继承了

ApplicationEventPublisher

接口。

Spring

中与事件有关的接口和类主要包括

ApplicationEvent



ApplicationListener





定义一个事件的类需要继承

ApplicationEvent

或者

ApplicationContextEvent

抽象类,

该抽象类中只有一个

构造函数,并且带有一个

Object

类型的参数作为事件源,并且该事件源不能为

null

,因此我们需要在自己

的构造函数中执行

super(Object)





public class UserEvent extends ApplicationEvent

{

  

private String eventContent;

  

public String getEventContent(){

     

return eventContent;

  

}

  

public void setEventContent(String eventContent){

     

this.eventContent = eventContent;

  

}

  

public UserEvent(Object source,String eventContent){

     

super(source);

     

this.eventContent = eventContent;

  

}

}

针对一种事件,可能需要特定的监听器,因此,监听器需要实现

ApplicationListener

接口。当监听器接收

到一个事件的时候,就会执行它的

onApplicationEvent()

方法。由于

Spring IoC

中的事件模型是一种简单

的、粗粒度的监听模型,当有一个事件到达时,所有的监听器都会接收到,并且作出响应,如果希望只针

对某些类型进行监听,需要在代码中进行控制。



public class UserListener implements ApplicationListener

{

  

public void onApplicationEvent(ApplicationEvent event){

     

if(event instanceof UserEvent){ //

只对

UserEvent

类型进行处理



        

UserEvent ue = (UserEvent)event;

        

String result = ue.getEventContent();

        

System.out.println("Event Content:"+result);

     

}

  

}

}

对于发布事件,我们可以实现

ApplicationContextAware

或者

ApplicationEventPublisherAware

接口。



public class UserBiz implements ApplicationContextAware

{

private ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException

{

   

this.applicationContext = applicationContext; 

}

public void service(String thing)

{












   

UserEvent event = new UserEvent(this,thing);

   

event.setEventContent("I shoud "+thing);

   

applicationContext.publishEvent(event);

}   

}

或者如下:



public class UserBiz2 implements ApplicationEventPublisherAware

{

private ApplicationEventPublisher applicationEventPublisher;

public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)

{

this.applicationEventPublisher = applicationEventPublisher;

}

public void service(String thing)

{

   

UserEvent event = new UserEvent(this,thing);

   

event.setEventContent("I shoud "+thing);

   

applicationEventPublisher.publishEvent(event);

}

}

至此便完成了事件的发布,当

ApplicationContext

接收到事件后,事件的广播是

Spring

内部给我们做的,

不需要了解具体的细节。其实在

Spring

读取配置文件之后,利用反射,将所有实现

ApplicationListener



Bean

找出来,注册为容器的事件监听器。当接收到事件的时候,

Spring

会逐个调用事件监听器。剩下要

做的就是在配置文件中配置监听器。



<bean class="footprint.spring.ioc.event.UserListener"/>

Spring

容器自身会发布一些事件,包括

ContextClosedEvent



ContextRefreshedEvent



ContextStartedEvent



ContextStoppedEvent





http://wenku.baidu.com/view/8b833790daef5ef7ba0d3c76.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值