【Spring】16 ApplicationContextAware 接口

本文介绍了Spring框架中的ApplicationContextAware接口,包括其作用、使用方法(创建并实现接口、配置Bean、启动类)、以及在获取其他Bean的引用和环境变量方面的应用场景。
摘要由CSDN通过智能技术生成

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 创建并实现接口

DemoBean.java

package org.example.cheney;

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

public class DemoBean implements ApplicationContextAware {
    private HelloBean helloBean;

    public void print(String name) {
        // DemoBean 类中的处理逻辑
        System.out.println("[DemoBean]  Hi: " + name);

        // HelloBean 类中的处理逻辑
        helloBean.say(name);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 通过 ApplicationContext 来获取 HelloBean 的引用
        this.helloBean = applicationContext.getBean(HelloBean.class);
    }
}

上面代码演示了如何通过实现 ApplicationContextAware 接口来获取 Spring 容器中的其他 Bean(在这里是 HelloBean),并在 DemoBean 类中使用这个引用执行业务逻辑。

HelloBean.java

package org.example.cheney;

public class HelloBean {

    public void say(String message) {
        System.out.println("[HelloBean] Hello: " + message);
    }
}

上面代码只有一个打印的 say 方法,实际开发时可以换成对应的逻辑

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="helloBean" class="org.example.cheney.HelloBean"/>
       <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) {
        String location = "applicationContext.xml";
        try (AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext(location)) {
            // 从容器中获取 DemoBean
            DemoBean demoBean = context.getBean(DemoBean.class);

            // 调用 DemoBean 类中的 print 方法
            demoBean.print("cheney");
        }
    }
}

3.4 启动

输出结果:

在这里插入图片描述

4. 应用场景

ApplicationContextAware 接口通常用于以下场景

  • 获取其他 Bean 的引用:

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

  • 获取环境变量:

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

总结

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值