Spring事件--Application Event

Spring 的事件爲Bean 與 Bean之間的消息通信提供了支持。當一個Bean處理完一個任務之後希望另外一個Bean知道並能做相應的處理,這時候我們就需要讓另外一個Bean監聽當前Bean所發送的事件。

Spring的事件需要遵循如下流程:

1.自定義事件,繼承Application Event類;

2.自定義監聽事件,實現ApplicationListener接口;

3.使用容器發佈時間。

Demo~

1.自定義事件

package com.test.event;

import org.springframework.context.ApplicationEvent;

/**
 * 自定義事件
 * Created by xian.juanjuan on 2017-6-12 09:27.
 */
public class DemoEvent extends ApplicationEvent{

    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;
    }
}

2.事件監聽器

package com.test.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 事件監聽器
 * Created by xian.juanjuan on 2017-6-12 09:30.
 */
@Component
public class DemoListener implements ApplicationListener<DemoEvent>{// 指定監聽的時間類型DemoEvent

    @Override
    public void onApplicationEvent(DemoEvent demoEvent){//對消息進行接收處理
        String msg = demoEvent.getMsg();
        System.out.println(this.getClass().getName()+"收到了"+msg);
    }
}

3.事件發佈

package com.test.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 事件發佈
 * Created by xian.juanjuan on 2017-6-12 09:37.
 */
@Component
public class DemoPublisher {

    @Autowired
    ApplicationContext applicationContext;//注入ApplicationContext用來發布事件

    public void publish(String msg){
        applicationContext.publishEvent(new DemoEvent(this,msg));//利用ApplicationContext的publishEvent來發布事件
    }
}

4.配置類

package com.test.event;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 配置類
 * Created by xian.juanjuan on 2017-6-12 09:41.
 */
@Configuration
@ComponentScan("com.test.event")
public class EventConfig {
}

5.運行

package com.test.event;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 執行類
 * Created by xian.juanjuan on 2017-6-12 09:42.
 */
public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
        DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);

        demoPublisher.publish("DemoPublisher發佈的消息:hello application event");
        context.close();
    }
}

結果

10:28:30.432 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'demoPublisher'
10:28:30.433 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'demoListener'
com.test.event.DemoListener收到了DemoPublisher發佈的消息:hello application event
10:28:30.435 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4de8b406: startup date [Mon Jun 12 10:28:28 CST 2017]; root of context hierarchy

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章