Spring Event

spring event是spring的事件机制,和gui中的事件类似,使用的也是观察者模式。spring event为bean与bean之间的消息通信提供了支持。当一个bean处理完一个任务后,希望另外一个bean知道并能做响应的处理,这时我们就需要让另一个bean监听当前bean所发送的事件。

 

这里有三个角色

事件发布者:消息发发布者

事件订阅者:可以有多个,订阅消息的人

事件:发生的事件,或者传递的信息

 

三个角色非常好理解,事件的发布者就是发布时间的人,也就是事件源,我们以gui中的按钮为例,当点击按钮的时候触发一个点击事件。这个过程中,按钮就是时间源,点击事件就是一个事件,记录的是点击的一些状态,那么点击的处理或者响应就是事件的订阅者,订阅者得到响应以后,处理一些事情。

 

理解清楚了这三种角色,下面来具体看代码的实现,都非常的简单

 

先定义事件

package com.hy.spring.test5;

import org.springframework.context.ApplicationEvent;

public class DemoEvent extends ApplicationEvent {

     private static final long serialVersionUID = -1916624710173721661L;

     private String msg;

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

     public String getMsg() {
          return msg;
     }

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

     public static long getSerialversionuid() {
          return serialVersionUID;
     }

}

再来定义第一个订阅者(订阅者是通过实现ApplicationListener<DemoEvent> 接口,并指定泛型参数来确定要监听的事件的,这个过程spring会给我加入到观察者的队列中)

package com.hy.spring.test5;

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

@Component
public class DemoListener1 implements ApplicationListener<DemoEvent> {
    @Override
    public void onApplicationEvent(DemoEvent event) {
        String msg = event.getMsg();
        System.out.println("1收到消息 " + msg);
    }
}

再来定义第二个订阅者

package com.hy.spring.test5;

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

@Component
public class DemoListener2 implements ApplicationListener<DemoEvent> {
    @Override
    public void onApplicationEvent(DemoEvent event) {
        String msg = event.getMsg();
        System.out.println("2收到消息 " + msg);
    }
}

再来定义发布者

package com.hy.spring.test5;

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

@Component
public class DemoPublisher {

    @Autowired
    private ApplicationContext applicationContext;

    public void publish(String msg) {
        applicationContext.publishEvent(new DemoEvent(this, msg));
    }

}

角色都定义好了,下面是spring的配置类

package com.hy.spring.test5;

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

@Configuration
@ComponentScan("com.hy.spring.test5")
public class EventConfig {

}

测试类

package com.hy.spring.test5;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
     public static void main(String[] args) {
          AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
          DemoPublisher p = context.getBean(DemoPublisher.class);
          p.publish("hello");
          context.close();
     }
}

运行结果

七月 19, 2017 4:06:52 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e2c634b: startup date [Wed Jul 19 16:06:52 CST 2017]; root of context hierarchy
1收到消息 hello
2收到消息 hello
七月 19, 2017 4:06:53 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e2c634b: startup date [Wed Jul 19 16:06:52 CST 2017]; root of context hierarchy

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值