【Spring】14 ApplicationEventPublisherAware 接口


Spring 框架为开发者提供了丰富的扩展点,其中之一是 Bean 生命周期中的回调接口。本文将专注介绍一个与事件发布相关的接口 ApplicationEventPublisherAware,探讨它的作用、用法,以及在实际开发中的应用场景。

1. 简介

在 Spring 中,ApplicationEventPublisherAware 接口是一个回调接口,它提供了一个用于设置 Bean 所在的ApplicationEventPublisher 的方法。当一个 Bean 实现了 ApplicationEventPublisherAware 接口时,在该 Bean 实例被实例化后,Spring 容器会调用 setApplicationEventPublisher 方法,并将该 Bean 所在的ApplicationEventPublisher 作为参数传递进去。

源码如下

在这里插入图片描述

2. 作用

ApplicationEventPublisherAware 主要用于获取发布事件的 ApplicationEventPublisher,使得 Bean 能够在运行时发布自定义的事件。

3. 使用

要让一个Bean实现 ApplicationEventPublisherAware 接口,需要按以下步骤进行

在这里插入图片描述

3.1 创建并实现接口
package org.example.cheney;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;

public class DemoBean implements ApplicationEventPublisherAware {
    private ApplicationEventPublisher eventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
        System.out.println("【ApplicationEventPublisherAware】: 通过 ApplicationEventPublisher 创建 Bean");
    }

    public void publishCustomEvent(String message) {
        // 创建自定义事件
        CustomEvent customEvent = new CustomEvent(this, message);
        // 发布事件
        eventPublisher.publishEvent(customEvent);
    }
}
// 自定义一个事件
class CustomEvent extends ApplicationEvent {
    private final String message;
    public CustomEvent(Object source, String message) {
        super(source);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}
// 自定义一个监听器
class EventListener implements ApplicationListener<CustomEvent> {
    @Override
    public void onApplicationEvent(CustomEvent event) {
        System.out.println("监听到的信息是:" + event.getMessage());
    }
}

3.2 配置 Bean 信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd"
       >
    <bean id="demoBean" class="org.example.cheney.DemoBean"/>
    <bean id="eventListener" class="org.example.cheney.EventListener"/>
</beans>

3.3 创建启动类
package org.example.cheney;

import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) throws Exception {
        String location = "applicationContext.xml";
        try (AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext(location)) {
            DemoBean demoBean = (DemoBean) context.getBean("demoBean");
            demoBean.publishCustomEvent("cheney");
            System.out.println("End.");
        }
    }
}
3.4 启动

输出结果:

在这里插入图片描述

3.5 工作流程图

在这里插入图片描述

4. 应用场景

ApplicationEventPublisherAware 接口通常用于以下场景:

  • 自定义事件发布:

    当一个 Bean 需要在运行时发布自定义的事件时,可以使用 ApplicationEventPublisher 发布事件

  • 事件驱动开发:

    在事件驱动的架构中,ApplicationEventPublisher 可以用于触发和响应事件,实现松耦合的组件之间的通信

总结

Spring 框架为开发者提供了丰富的扩展点,其中之一是 Bean 生命周期中的回调接口。ApplicationEventPublisherAware 接口为开发者提供了一种简单而有用的方式来获取 Bean 所在的ApplicationEventPublisher,从而实现对事件的发布。通过实现该接口,Bean 可以在初始化阶段获取ApplicationEventPublisher,并使用它发布自定义事件。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值