IOC核心实现原理

IOC核心实现原理


前言

IOC容器核心实现原理


一、Definition

注解配置Bean还是XML方式配置,都要经过一个过程就是创建这些类对应BeanDefinition【BeanDefinition是spring用来描述一个类的,类似于Java描述一个类是Class类对象】,spring通过BeanDefinition来实例化Bean
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、BeanFactory、ApplicationContext

AppliationContext继承了BeanFactory,ApplicationContext提供了比BeanFactory更丰富的功能,BeanFactory默认实现类DefaultListableBeanFactory

三、Bean属性赋值阶段

在根据类信息转载成BeanDefinition对象时不会对属性进行处理,而是借助BeanWrapperBean包装类对属性进行初始化。是在一个Bean对象创建之后将其装载成一个BeanWrapper对象。

PostProcessor后置处理器增强处理器用于扩展功能使用

PostProcesser(Interface)function
BeanDefinitionRegistryPostProcessorExtension to the standard BeanFactoryPostProcessor SPI, allowing for the registration of further bean definitions
BeanFactoryPostProcessorcustom modification of an application context’s bean definitions
BeanPostProcessorcustom modification of new bean instances — for example, checking for marker interfaces or wrapping beans withproxies
abstractfunction
PlaceholderConfigurerSupport BeanFactoryPostProcesser抽象类Abstract base class for property resource configurers that resolve placeholders(占位符) in bean definition property values
implementsfunction
ConfigurationClassPostProcessor BeanDefinitionRegistryPostProcessor实现类
扩展:springboot自动装配原理
This post processor is priority-ordered as it is important that any @Bean methods declared in @Configuration classes have their corresponding bean definitions registered before any other BeanFactoryPostProcessor executes

在这里插入图片描述
在调用BeanFactoryPostProcesser之前属性值如下

调用invokeBeanFactoryPostProcessors(beanFactory);这个方法之后,属性为
在这里插入图片描述

  • 自定义BeanFactory后置处理器(可以定义多个,可以@order设置优先级)
实现接口BeanFactoryPostProcesser 重写方法,在实现类加上@Component

四、初始化Bean属性


1、实例化
这个过程创建Bean对象
2、初始化

知识储备: 比如spring内置属性BeanFactory 、ApplicationContext,这两个属性要怎么初始化赋值? ps:容器对象不止这两个

  • Aware接口没有如何方法
    • BeanFactoryAware接口 setBeanFactory()
    • ApplicationContextAware接口setApplicationContextAware()
      这两个方法可以在Bean初始化阶段被spring调用进行BeanFactory、ApplicationContext,这两个属性初始化反射方式

属性赋值分为用户自定义属性赋值,容器对象赋值【通过set方法】

容器对象对应set方法所在接口如下

在这里插入图片描述

问题:既然是set方法,那这些容器对象能不能我们为其写个set方法来实现注入?

在这里插入图片描述

五、Bean扩展

BeanPostProcesser Bean后置处理器 两个方法

  • postProcessBeforeInitialization 初始化前置处理器
  • 执行初始化
  • postProcessAfterInitialization 初始化后置处理器

AOP【基于代理】是在Bean扩展进行实现,其一实现类`
在这里插入图片描述

在这里插入图片描述

下面这张图片中初始化的步骤更准确是执行初始化方法init-method其实是可以用来进行属性初始化赋值,但是在上面属性已经初始化了,这个可以使用。spring是通过invokeInitMethods调用。

在这里插入图片描述

注解@AutoWired @Value在Bean生命周期哪个部分

spring扩展部分留在BeanPostProcesser

六、BeanFactory、FactoryBean

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值