栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

一步一步教你使用Spring框架中的事件订阅发布

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一步一步教你使用Spring框架中的事件订阅发布

1. 前言

Java 开发中有些逻辑是这样的,完成了A操作,再继续B操作,在继续C操作。这么描述好像有点不清楚。打个比方把,你吃晚饭,通知你老婆(女友)来收碗筷,然后通知你的线上兄弟告诉他们你回来了准备开黑。至于你老婆(女友)来不来收拾无所谓,反正你告诉她了。至于你兄弟你也是通知他们,人家也不一定组你,万一他们正在跟一个一拖三的carry大佬玩的正起劲儿呢。

2. 事件的概念

吃晚饭就是一个所谓的事件。触发了随后的两个操作,他们只存在因果关系。但是它们互不干扰,各自为政。一个完整的事件由 事件源、事件发布、事件监听 组成。 接下来我们聊聊 Spring 中的事件。

3. Spring 中的事件

Spring 框架中使用了大量的事件机制,比如 Spring Boot 的启动。方便起见我们新建一个 Spring Boot 工程。然后跟着我一步步的来进行事件的操作。

3.1 声明事件
  • 声明一个事件。通过继承 org.springframework.context.ApplicationEvent 来编写事件。时间里定义好事件推送到监听器需要执行的方法,当然也可以在监听器里写触发逻辑。我们来声明一下:
package cn.felord.boot.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;


@Slf4j
public class EatEvent extends ApplicationEvent {
    private Boolean eatFinished;


    
    public EatEvent(Boolean eatFinished) {
 super(eatFinished);
 this.eatFinished = eatFinished;
    }

    
    public void callGirlFriend() {
 log.info("亲爱的! 我吃完饭了,来收拾收拾吧");
    }

    
    public void callBrothers() {
 log.info("兄弟们! 我吃完饭了,带我开黑");
    }

    
    public Boolean isEatFinished() {
 return this.eatFinished;
    }
}
3.2 事件发布

发布事件通过实现事件发布接口 org.springframework.context.ApplicationEventPublisher 或者其门面接口 org.springframework.context.ApplicationEventPublisherAware, 推荐门面接口,里面要定义一个主动推送事件的方法如下面的 refreshEvent 方法,实际代理了 ApplicationEventPublisher 执行其 publishEvent 方法:

package cn.felord.boot.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;


@Slf4j
public class EatEventPublisherAware implements ApplicationEventPublisherAware {
    private ApplicationEventPublisher applicationEventPublisher;

    private ApplicationEvent eatEvent;

    public EatEventPublisherAware(ApplicationEvent eatEvent) {
 this.eatEvent = eatEvent;
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
 this.applicationEventPublisher = applicationEventPublisher;
    }

    
    public void refreshEvent() {
 log.info("发送事件中……");
 this.applicationEventPublisher.publishEvent(eatEvent);
    }

}
3.3 事件监听

事件监听用来监听事件以触发相关的逻辑。通过实现 org.springframework.context.ApplicationListener 来实现事件的监听。特别注意泛型E,如果不指定事件将可以接收任何事件,尽量职责单一。

package cn.felord.boot.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;


@Slf4j
public class EatEventListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(EatEvent eatEvent) {
 //如果吃完饭了
 if (eatEvent.isEatFinished()) {
     eatEvent.callGirlFriend();
     log.error("来自母老虎的咆哮:滚犊子");
     eatEvent.callBrothers();
     log.error("太晚了,我们已经满了,明天带你");
     log.info("还是关注一下 【码农小胖哥】 学习点新知识吧");
 }
    }
}
3.4 注入Spring IoC

将上面三个类注入 Spring 容器中,这里我们采用了 JavaConfig 方式,看起来更明显。

package cn.felord.boot.config;

import cn.felord.boot.event.EatEvent;
import cn.felord.boot.event.EatEventListener;
import cn.felord.boot.event.EatEventPublisherAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class EventConfig {

    @Bean
    public ApplicationEvent eatEvent() {
 return new EatEvent(true);
    }

    @Bean
    public ApplicationListener eatEventListener() {
 return new EatEventListener();
    }

    @Bean
    public ApplicationEventPublisherAware eatEventPublisherAware(ApplicationEvent eatEvent) {
 return new EatEventPublisherAware(eatEvent);
    }

}
4. 测试

这里就大功告成了,那么如何使用呢,执行事件发布器的发布方法 refreshEvent 就行了,事件监听器监听到事件会自动响应。我们来写一个单元测试。

package cn.felord.boot;

import cn.felord.boot.event.EatEventPublisherAware;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EventSpringApplicationTests {
@Resource
private EatEventPublisherAware eatEventPublisherAware;
    @Test
    public void contextLoads() {

 eatEventPublisherAware.refreshEvent();
    }

}

运行一下,入图

到此你应该就学会使用 Spring 事件了,这样写出来的代码逼格更高。还能提现你对 Spring 框架的一些理解。当然还有一种更加简单的、基于注解的方式,这里不再阐述。相关代码在我的 码云仓库

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号