【Spring】16 ApplicationContextAware 接口

2024-01-01 15:28:00

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 的引用、获取环境变量等。

文章来源:https://blog.csdn.net/yanyc0411/article/details/135324736
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。