Spring中的事件与监听

 

一、首先看一下Spring中的Event。其定义如下:

package org.springframework.context;

import java.util.EventObject;

/**

 * Class to be extended by all application events.

 * Abstract as it doesn't make sense for generic events

 * to be published directly.

*/

public abstract class ApplicationEvent extends EventObject {

    /** use serialVersionUID from Spring 1.2 for interoperability */

    private static final long serialVersionUID = 7099057708183571937L;

    /** System time when the event happened */

    private final long timestamp;

    /**

     * Create a new ApplicationEvent.

     * @param source the component that published the event (never <code>null</code>)

     */

    public ApplicationEvent(Object source) {

       super(source);

       this.timestamp = System.currentTimeMillis();

    }

    /**

     * Return the system time in milliseconds when the event happened.

     */

    public final long getTimestamp() {

       return timestamp;

    }

}

这是一个抽象类,定义了Spring中的Event。其构造函数的参数表示事件源。我们可以根据自己的需要定义事件类型,只要继承自ApplicationEvent即可。

public class ActionEvent extends ApplicationEvent {

    public ActionEvent(Object source){

       super(source);

    }

}

 

 

 

 

 

二、有了事件,必然要有监听器。这里的监听器为ApplicationListener

还是先看下该类的定义:

package org.springframework.context;

import java.util.EventListener;

/**

 * Interface to be implemented by application event listeners.

 * Based on the standard <code>java.util.EventListener</code> interface

 * for the Observer design pattern.

 */

public interface ApplicationListener extends EventListener {

    /**

     * Handle an application event.

     * @param event the event to respond to

     */

    void onApplicationEvent(ApplicationEvent event);

}

此接口只定义了一个方法,即当接收到事件发生的通知时,所需要执行的动作。

可以通过实现此接口来完成用户自定义的监听器:

public class ActionListener implements ApplicationListener {

    public void onApplicationEvent(ApplicationEvent event) {

       if(event instanceof ActionEvent){

           System.out.print(event.getSource());

       }

    }

}

三、有了事件和监听器,那么如何发布这个事件呢?需要了解的是,在Spring中,通过Application接口中的publishEvent()方法可以发布事件。而在普通应用中可以通过实现ApplicationContextAware接口可以轻松的获取ApplicaitonContext的实例。

下面首先来看一下ApplicationContextAware接口的定义:

public interface ApplicationContextAware {

    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

通常情况下,需要获取较多的bean,或者是需要访问资源文件以及想要发布ApplicationEvent时实现此接口。

 

 

 

用户可以根据需要实现此接口,并在程序中恰当的时候发布事件。

public class LoginAction implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext arg0)

           throws BeansException {

       this.applicationContext = arg0;

    }

    public int login(String username,String password){

       SimpleClass sc = new SimpleClass("1","simpleClass");

       ActionEvent actionEvent = new ActionEvent(sc);

       this.applicationContext.publishEvent(actionEvent);

       return 0;

    }

}

四、上面相继给出了事件、监听器及其关系的介绍和简单举例,下面给出简单测试的代码:

public class Test {

    public static void main(String[] args) {

ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");

       LoginAction action = (LoginAction)cxt.getBean("loginAction");

       action.login("prince", "mimamima");

    }

}

当发生login动作时,终端会有如下输出:This is SimpleClass

总结:纵观Spring的事件与监听,其实现相对简单。首先是定义事件(实现ApplicationEvent),接着要定义监听器(实现ApplicationListener接口)。最后,需要发布事件的类要实现ApplicationContextAware接口,并用其中的ApplicationContext的实例发布事件publish(source).

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值