首页 > 移动平台 > 详细

10点睛Spring4.1-Application Event

时间:2015-05-19 02:10:03      阅读:303      评论:0      收藏:0      [点我收藏+]

10.1 Application Event

  • Spring使用Application Event给bean之间的消息通讯提供了手段
  • 应按照如下部分实现bean之间的消息通讯
    • 继承ApplicationEvent类实现自己的事件
    • 实现继承ApplicationListener接口实现监听事件
    • 使用ApplicationContext发布消息

10.2示例

示例中的通讯两个bean分别为DemoListener和Main

10.2.1 编写自定义的ApplicationContext

package com.wisely.event;

import org.springframework.context.ApplicationEvent;

public class DemoEvent extends ApplicationEvent{
    private static final long serialVersionUID = 1L;
    private String msg;

    public DemoEvent(Object source,String msg) {
        super(source);
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

10.2.2 编写实现ApplicationListener的类

package com.wisely.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {

    public void onApplicationEvent(DemoEvent event) {
            String msg = ((DemoEvent) event).getMsg();
            System.out.println("我监听到了pulisher发布的message为:"+msg);

    }


}

10.2.3 测试

package com.wisely.event;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext("com.wisely.event");
        Main main =context.getBean(Main.class);
        main.pulish(context);
        context.close();

    }
    public void pulish(AnnotationConfigApplicationContext context){
        context.publishEvent(new DemoEvent(this, "22"));
    }

}

输出结果

我监听到了pulisher发布的message为:22

10点睛Spring4.1-Application Event

原文:http://wiselyman.iteye.com/blog/2212013

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!