Spring的实践、监听和发布

Spring的事件(Applicaction Event)为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务后,希望另外一个Bean知道并且能够做响应的处理,这是我们需要让另外一个Bean监听当前Bean所发送的事件。

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

(1)自定义事件,继承ApplicationEvent

(2)定义事件监听器,实现ApplicationListener

(3)使用容器发布事件


示例如下:

1、自定义事件

package com.lwh.highlight_spring4.ch2.event;

/**
 * Created by luwenhu on 2017/9/20.
 */

import org.springframework.context.ApplicationEvent;

/**
 * Spring的事件为Bean与Bean之间的消息通信提供了支持,当Bean处理完一个任务后,希望另外一个Bean知道并且能做响应的处理
 * 这时我们需要让另外一个Bean监听当前Bean所发送的事件
 * 流程:
 * (1)自定义事件,继承ApplicationEvent
 * (2)定义事件监听器,实现ApplicationListener
 * (3)使用容器发布事件
 */

//(1)自定义一个事件
public class DemoEvent extends ApplicationEvent {
    private static final long serialVersionUID = 1L;
    private String msg;

    public String getMsg() {
        return msg;
    }

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

    /**
     * Create a new ApplicationEvent.
     *
     * @param source the component that published the event (never {@code null})
     */
    public DemoEvent(Object source,String msg) {
        super(source);
        this.msg = msg;

    }
}

2、事件监听器

package com.lwh.highlight_spring4.ch2.event;

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

/**
 * Created by luwenhu on 2017/9/20.
 */
//(2)事件监听器

@Component
public class DemoListener implements ApplicationListener<DemoEvent>{

    public void onApplicationEvent(DemoEvent event) {
        String msg = event.getMsg();
        System.out.println("我(bean-demoListener)接收到bean-demoPublisher发布的消息:"+msg);
    }
}

3、事件发布类

package com.lwh.highlight_spring4.ch2.event;

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

/**
 * Created by luwenhu on 2017/9/20.
 */
//(3)事件的发布类
@Component
public class DemoPublisher {

    @Autowired//注入Application用来发布事件
    ApplicationContext applicationContext;
    public void publish(String msg){
        applicationContext.publishEvent(new DemoEvent(this,msg));
    }

}

4、配置类

package com.lwh.highlight_spring4.ch2.event;

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

/**
 * Created by luwenhu on 2017/9/20.
 */
@Configuration
@ComponentScan("com.lwh.highlight_spring4.ch2.event")
public class EventConfig {

}

5、运行

package com.lwh.highlight_spring4.ch2.event;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by luwenhu on 2017/9/20.
 */
public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);

        DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);

        demoPublisher.publish("hello application event");

        context.close();
    }
}

运行结果:

原文地址:https://www.cnblogs.com/wenhulu/p/7560204.html