【Spring】14 ApplicationEventPublisherAware 接口

文章目录

      • 1. 简介
      • 2. 作用
      • 3. 使用
        • 3.1 创建并实现接口
        • 3.2 配置 Bean 信息
        • 3.3 创建启动类
        • 3.4 启动
        • 3.5 工作流程图
      • 4. 应用场景
      • 总结

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

1. 简介

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

源码如下

【Spring】14 ApplicationEventPublisherAware 接口_第1张图片

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 信息

<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 工作流程图

【Spring】14 ApplicationEventPublisherAware 接口_第2张图片

4. 应用场景

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

  • 自定义事件发布:

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

  • 事件驱动开发:

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

总结

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

你可能感兴趣的:(Spring,spring,java,后端)