当前位置: 首页 > news >正文

Spring Aware接口执行时机

一. 介绍

Spring Aware 接口的执行时机有两处,都在 getBean() 中的 initializeBean() 中;

下面我们分析这两处时机;

// ----------------------- AbstractAutowireCapableBeanFactory ---------------------
protected Object initializeBean(String beanName, Object bean, RootBeanDefinition mbd) {// 1. 时机一,invokeAwareMethods()invokeAwareMethods(beanName, bean);Object wrappedBean = bean;if (mbd == null || !mbd.isSynthetic()) {// 2. 时机二,BeanPostProcessor 的 postProcessBeforeInitialization()// 处理 aware bean 的是 ApplicationContextAwareProcessorwrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try {// 执行 bean 的 init-methodinvokeInitMethods(beanName, wrappedBean, mbd);}catch (Throwable ex) {throw new BeanCreationException((mbd != null ? mbd.getResourceDescription() : null), beanName, ex.getMessage(), ex);}if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean;
}

二. invokeAwareMethods()

时机一,我们看下这个方法会处理几种 Aware bean 对象;

会处理三种 Aware bean,分别是:BeanNameAware、BeanClassLoaderAware、BeanFactoryAware;

// ----------------------- AbstractAutowireCapableBeanFactory ---------------------
private void invokeAwareMethods(String beanName, Object bean) {if (bean instanceof Aware) {if (bean instanceof BeanNameAware beanNameAware) {beanNameAware.setBeanName(beanName);}if (bean instanceof BeanClassLoaderAware beanClassLoaderAware) {ClassLoader bcl = getBeanClassLoader();if (bcl != null) {beanClassLoaderAware.setBeanClassLoader(bcl);}}if (bean instanceof BeanFactoryAware beanFactoryAware) {beanFactoryAware.setBeanFactory(AbstractAutowireCapableBeanFactory.this);}}
}

三. ApplicationContextAwareProcessor

ApplicationContextAwareProcessor 是一个处理 Aware 接口的 BeanPostProcessor,我们直接看它的 postProcessBeforeInitialization();

// --------------------------- ApplicationContextAwareProcessor -----------------------------
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||bean instanceof ApplicationStartupAware)) {return bean;}invokeAwareInterfaces(bean);return bean;
}// --------------------------- ApplicationContextAwareProcessor -----------------------------
private void invokeAwareInterfaces(Object bean) {if (bean instanceof Aware) {// EnvironmentAware:系统环境if (bean instanceof EnvironmentAware environmentAware) {environmentAware.setEnvironment(this.applicationContext.getEnvironment());}if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);}if (bean instanceof ResourceLoaderAware resourceLoaderAware) {resourceLoaderAware.setResourceLoader(this.applicationContext);}// ApplicationEventPublisherAware:事件发布if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);}if (bean instanceof MessageSourceAware messageSourceAware) {messageSourceAware.setMessageSource(this.applicationContext);}if (bean instanceof ApplicationStartupAware applicationStartupAware) {applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());}// ApplicationContextAware:Spring 的 ApplicationContext 容器if (bean instanceof ApplicationContextAware applicationContextAware) {applicationContextAware.setApplicationContext(this.applicationContext);}}
}

http://www.mrgr.cn/news/2136.html

相关文章:

  • 数据结构与算法 - 贪心算法
  • 使用 Python 进行 PDF 文件加密
  • 03 网络编程 TCP传输控制协议
  • GAMES104:08游戏引擎的动画技术基础-学习笔记
  • 如何将 Bamboo agent 能力迁移到极狐GitLab tag 上?
  • 【编程知识】如何有趣的理解变量的数据类型和数值
  • 以太网交换基础
  • Ubuntu24.04使用SRS 搭建 RTMP流媒体服务器
  • Linux进程间通信——SystemV消息队列与信号量
  • NextJS 使用 Docker 发布
  • 算法-IMM
  • RK3568平台开发系列讲解(UART篇)line discipline
  • NIO中的异步—ChannelFuture、CloseFuture以及异步提升在NIO中的应用
  • MySQL中处理JSON数据:大数据分析的新方向,MYSQL如何处理JSON数据,参数讲解+实战案例+全网最全
  • wo是如何克服编程学习中的挫折感的?
  • roles
  • 力学笃行(五)Qt key绑定、钩子(hook)
  • Go-Zero微服务框架下开发接口流程
  • 【深度学习】OCR, 如何使用 Tesseract 进行 OCR 识别
  • spring揭秘07-aop基本要素及代理模式3种实现