Young Kbt blog Young Kbt blog
首页
  • java基础

    • Java基础
    • Java集合
    • Java反射
    • JavaJUC
    • JavaJVM
  • Java容器

    • JavaWeb
  • Java版本新特性

    • Java新特性
  • SQL 数据库

    • MySQL
    • Oracle
  • NoSQL 数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • ActiveMQ
    • RabbitMQ
    • RocketMQ
    • Kafka
  • 进阶服务

    • Nginx
  • Spring
  • Spring Boot
  • Spring Security
  • 设计模式
  • 算法
  • 知识
  • 管理

    • Maven
    • Git
  • 部署

    • Linux
    • Docker
    • Jenkins
    • Kubernetes
  • 进阶

    • TypeScript
  • 框架

    • React
    • Vue2
    • Vue3
  • 轮子工具
  • 项目工程
  • 友情链接
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
    • Vue2-Admin (opens new window)
    • Vue3-Admin(完善) (opens new window)
GitHub (opens new window)

Shp Liu

朝圣的使徒,正在走向编程的至高殿堂!
首页
  • java基础

    • Java基础
    • Java集合
    • Java反射
    • JavaJUC
    • JavaJVM
  • Java容器

    • JavaWeb
  • Java版本新特性

    • Java新特性
  • SQL 数据库

    • MySQL
    • Oracle
  • NoSQL 数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • ActiveMQ
    • RabbitMQ
    • RocketMQ
    • Kafka
  • 进阶服务

    • Nginx
  • Spring
  • Spring Boot
  • Spring Security
  • 设计模式
  • 算法
  • 知识
  • 管理

    • Maven
    • Git
  • 部署

    • Linux
    • Docker
    • Jenkins
    • Kubernetes
  • 进阶

    • TypeScript
  • 框架

    • React
    • Vue2
    • Vue3
  • 轮子工具
  • 项目工程
  • 友情链接
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
    • Vue2-Admin (opens new window)
    • Vue3-Admin(完善) (opens new window)
GitHub (opens new window)
  • Spring

  • Spring MVC

  • Spring Boot

    • Spring Boot - 生命周期与事件
    • Spring Boot - 事件驱动
    • Spring Boot - Bean 加载方式
    • Spring Boot - Aware
      • 前言
      • 常用 Aware
      • 功能实现原理
      • 使用
    • Spring Boot - 多环境配置
    • Spring Boot - 定时任务
    • Spring Boot - 异步任务
    • Spring Boot - 内置日志
    • Spring Boot - 函数式 Web
    • Spring Boot - 响应式远程调用
    • Spring Boot - 接口文档
    • Spring Boot - 单元测试
    • Spring Boot - 内容协商
    • Spring Boot - 参数校验
    • Spring Boot - RestTemplate
    • Spring Boot - 映射请求
    • Spring Boot - 请求参数接收
    • Spring Boot - 通用响应类
    • Spring Boot - 全局异常处理
  • Spring Security

  • Spring Cloud

  • Spring生态
  • Spring Boot
Young Kbt
2023-11-02
目录

Spring Boot - Aware

  • 前言
  • 常用 Aware
  • 功能实现原理
  • 使用

# 前言

Spring 框架的优点,在 Bean 中感知不到容器的存在,我们在不用代码中引入容器代码,但是在有些场景中我们需要感知到容器的存在,需要获取容器的工具类,这个时候需要使用 Spring Aware 提供的工具类

Spring 框架中提供许多实现 Aware 接口的类,这些类主要是为了辅助 Spring 访问容器中的数据。

# 常用 Aware

类名 作用
BeanNameWare 获得容器中 Bean 名称
BeanClassLoaderAware 获得类加载器
BeanFactoryAware 获得 Bean 创建工厂,可以获取 Bean
EnvironmentAware 获得 Environment 环境变量
EmbeddedValueResolverAware 获得 Spring 容器加载的 properties 文件属性值
ResourceLoaderAware 获得资源加载器
ApplicationEventPublisherAware 获得应用事件发布器
MessageSourceAware 获得文本信息
ApplicationContextAware 获得当前应用上下文,可以获取 Bean、Environment 等信息,是最常用的 Aware

这些 Aware 接口可以通过实现对应的接口,并在相应的回调方法中获取所需的资源或进行自定义的操作。通过使用这些 Aware 接口,我们可以更好地集成 Spring Boot 应用程序与其他组件或模块,并实现特定的业务逻辑或功能扩展。

# 功能实现原理

在 Bean 的创建和初始化过程中,当检测到 Bean 实现了某个 Aware 接口时,会在相应的阶段调用回调方法。

Spring 容器内部会维护一个集合,记录所有实现了 Aware 接口的 Bean。

在适当的生命周期阶段,例如在 Bean 的实例化后、属性注入前或初始化前,Spring 容器会遍历这个集合,并为每个实现了 Aware 接口的 Bean 调用相应的回调方法。

回调方法会传入对应的资源对象,例如 ApplicationContext、Environment 等,以供应用程序进行使用或操作。

应用程序可以在回调方法中根据自己的需求获取所需的资源,并进行进一步的处理或使用。

# 使用

BeanFactoryAware

获取 Bean 工厂

@Component
public class MyBeanFactoryAware implements BeanFactoryAware {
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        // 获取容器的某个 Bean
        System.out.println("BeanFactoryAware:" + beanFactory.getBean(AccountService.class));
    }
}
1
2
3
4
5
6
7
8

输出:

BeanFactoryAware:cn.youngkbt.boot.service.AccountService@3af7d855
1

BeanNameAware

获取当前 Bean 的名字

@Component
public class MyBeanNameAware implements BeanNameAware {
    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware:" + name);
    }
}
1
2
3
4
5
6
7

输出:

BeanNameAware:myBeanNameAware
1

EnvironmentAware

获取环境变量,也就是 application 相关配置信息。

@Component
public class MyEnvironmentAware implements EnvironmentAware {
    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("EnvironmentAware:" + environment.getProperty("server.port"));
    }
}

1
2
3
4
5
6
7
8

输出:

EnvironmentAware:8080
1

ApplicationContextAware

获取容器上下文

@Component
public class MyApplicationContextAware implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware:" + applicationContext.getBean(CouponService.class));
        System.out.println("ApplicationContextAware:" + applicationContext.getEnvironment().getProperty("server.port"));
    }
}
1
2
3
4
5
6
7
8

输出:

ApplicationContextAware:cn.youngkbt.boot.service.CouponService@178f268a
ApplicationContextAware:8080
1
2

ResourceLoaderAware

获取资源加载器

@Component
public class MyResourceLoaderAware implements ResourceLoaderAware {
    
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        Resource resource = resourceLoader.getResource("classpath:application.yml");
        System.out.println("ResourceLoaderAware:" + resource.getDescription());
    }
}
1
2
3
4
5
6
7
8
9

输出:

ResourceLoaderAware:class path resource [application.yml]
1
编辑此页 (opens new window)
#Spring Boot
更新时间: 2024/01/06, 06:51:53
Spring Boot - Bean 加载方式
Spring Boot - 多环境配置

← Spring Boot - Bean 加载方式 Spring Boot - 多环境配置→

最近更新
01
技术随笔 - Element Plus 修改包名 原创
11-02
02
Reactor - 扩展性
11-02
03
Reactor - 最佳实践
11-02
更多文章>
Theme by Vdoing | Copyright © 2021-2024 Young Kbt | blog
桂ICP备2021009994号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式