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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值