【Spring】15 ApplicationContextAware 接口

Spring 框架提供了许多回调接口,用于在 Bean 的生命周期中执行特定的操作。ApplicationContextAware 接口是其中之一,它允许 Bean 获取对 ApplicationContext 的引用。本文将介绍 ApplicationContextAware 接口的作用、使用方式,以及在实际应用中的常见场景。

1. 简介

ApplicationContextAware 是一个回调接口,用于在 Spring 容器实例化 Bean 后,将容器的上下文(ApplicationContext)传递给实现了该接口的 Bean。通过这个接口,Bean 可以获得对 Spring 容器的引用,从而获取容器中的其他 Bean 和资源。

源码如下

在这里插入图片描述

2. 作用

ApplicationContextAware 主要用于

  • 获取 ApplicationContext

    允许 Bean 在运行时获取对 Spring 容器的引用。

  • 与容器交互

    Bean 可以通过 ApplicationContext 与容器进行交互,例如获取其他 Bean 的引用、获取环境变量等。

3. 使用

要使用 ApplicationContextAware 接口,需要按以下步骤进行:

在这里插入图片描述

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class DemoBean implements ApplicationContextAware {
   
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
   
        this.applicationContext = applicationContext;
    }

    public void displayBeanNames() {
   
        // 打印 Bean 的名称
        String[] beanNames = applicationContext.getBeanDefinitionNames();
        System.out.println("【ApplicationContextAware】ApplicationContext 容器内存在的 Bean 的名字是:");
        for (String beanName : beanNames) {
   
            System.out.println(beanName);
        }
    }
}
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"/>
</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.displayBeanNames();
            System.out.println("End.");
        }
    }
}
3.4 启动

输出结果:

在这里插入图片描述

4. 应用场景

ApplicationContextAware 接口通常用于以下场景

  • 获取其他 Bean 的引用:

    当一个 Bean 需要与容器中的其他 Bean 进行交互时,可以使用 ApplicationContext 获取其他 Bean 的引用。

  • 获取环境变量:

    Bean 可以通过 ApplicationContext 获取容器的环境变量,例如配置文件中的属性值。

总结

Spring 框架提供了许多回调接口,用于在 Bean 的生命周期中执行特定的操作。通过实现 ApplicationContextAware 接口,Spring 提供了一种便捷的方式让 Bean 获取对 Spring 容器的引用。这使得 Bean 可以在运行时与容器进行交互,获取其他 Bean 的引用、获取环境变量等。

相关推荐

  1. ApplicationContextAware使用【工具类】

    2023-12-21 16:44:06       37 阅读
  2. Spring核心接口:ObjectProvider接口

    2023-12-21 16:44:06       46 阅读
  3. spring的扩展接口

    2023-12-21 16:44:06       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 16:44:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 16:44:06       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 16:44:06       82 阅读
  4. Python语言-面向对象

    2023-12-21 16:44:06       91 阅读

热门阅读

  1. Springboot Async 引起的循环依赖

    2023-12-21 16:44:06       55 阅读
  2. 云服务器的优缺点对比

    2023-12-21 16:44:06       58 阅读
  3. 第十五章 Linux系统日志管理

    2023-12-21 16:44:06       35 阅读
  4. 前端验收测试驱动开发

    2023-12-21 16:44:06       50 阅读
  5. 【Linux】Linux运维必备:groupdel命令的使用方法

    2023-12-21 16:44:06       60 阅读
  6. C++程序设计语言-郑莉 第三章实验报告及代码

    2023-12-21 16:44:06       59 阅读
  7. 语义信息简单理解

    2023-12-21 16:44:06       70 阅读
  8. Mysql笔记

    2023-12-21 16:44:06       53 阅读
  9. Flink系列之:Apache Kafka SQL 连接器

    2023-12-21 16:44:06       43 阅读
  10. C++初阶-反向迭代器的模拟实现

    2023-12-21 16:44:06       51 阅读
  11. c++打怪游戏(1.1.0)

    2023-12-21 16:44:06       42 阅读