Spring之事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/carson0408/article/details/86316371

        Spring的事件为Bean与Bean之间的消息通信提供了支持。当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时就需要另一个Bean监听当前Bean所发送的事件。

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

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

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

(3).使用容器发布事件。

1.自定义事件

        自定义事件需要继承ApplicationEvent,声明是一个事件,可以被监听。

package com.carson.demoevent;

import org.springframework.context.ApplicationEvent;

/**
 * 自定义事件
 * handle()方法用于对发布的msg进行修改,作为一个任务。
 */
public class MyEvent extends ApplicationEvent {

    private static final long serialVersionUID=1L;
    private String msg;

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

    public String getMsg() {
        return msg;
    }

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

    public String handle(){
        return "The infomation is:"+msg.toUpperCase();
    }
}

        MyEvent会处理发布的消息并被监听器监听。

2.事件监听器

        该事件监听器实现了ApplicationListener,并指定了自定义的事件类型,通过onApplicationEvent方法对消息进行接受处理。即获取自定义事件中处理过的消息。

package com.carson.demoevent;

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

/**
 * 实现ApplicationListener接口,并指定监听的事件类型,这里可以指定自定义事件类型
 * 使用ApplicationListener接口的方法onApplicationEvent方法对消息进行接收处理
 */
@Component
public class MyListener implements ApplicationListener<MyEvent> {
    public void onApplicationEvent(MyEvent myEvent) {

        String info = myEvent.handle();
        System.out.println("MyListener接收到了MyPublisher发布的消息:"+info);

    }
}

3.事件发布类

        该类通过注入ApplicationContext来进行事件发布,并使用其publishEvent方法来发布,即将自定义事件与事件发布进行关联起来,给外界提供一个接口。

package com.carson.demoevent;


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

@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;//注入ApplicationContext用来发布事件

    public void publish(String msg){
        applicationContext.publishEvent(new MyEvent(this,msg));//使用ApplicationContext的publishEvent方法来发布。
    }


}

4.配置类

        这里通过配置类取代xml配置,只要用@Configuration来注明这是个注解类,另外@ComponentScan来确认扫描的包。

package com.carson.demoevent;

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

@Configuration
@ComponentScan("com.carson.demoevent") //扫描包
public class EventConfig {
}

5.运行类

        运行类中通过容器来生成Bean并获取,这里因为使用配置类进行配置,所以使用AnnotationApplicationContext容器。

package com.carson.demoevent;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
        MyPublisher myPublisher = context.getBean(MyPublisher.class);

        myPublisher.publish("the earth is round");
    }
}

运行之后,结果如下:

猜你喜欢

转载自blog.csdn.net/carson0408/article/details/86316371