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

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