# Spring IoC依赖来源 ## 1. 依赖查找的来源 查找来源 ![img](assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvbmdoYW8wMDc=,size_16,color_FFFFFF,t_70#pic_center.png) Spring 內建 BeanDefintion ![img](assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvbmdoYW8wMDc=,size_16,color_FFFFFF,t_70#pic_center-16652818457652.png) Spring 內建单例对象 ![img](assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvbmdoYW8wMDc=,size_16,color_FFFFFF,t_70#pic_center-16652818587724.png) ## 2. [依赖注入](https://so.csdn.net/so/search?q=依赖注入&spm=1001.2101.3001.7020)的来源 注入来源 ![img](assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvbmdoYW8wMDc=,size_16,color_FFFFFF,t_70#pic_center-16652818823628.png) * 单例对象由 `org.springframework.beans.factory.config.SingletonBeanRegistry` 注册,`org.springframework.beans.factory.support.AbstractBeanFactory` 实现了这个接口,从容器中获取 Bean 的方法 `org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean` 中,会先从单例对象中查找,如果查找到,直接返回;查找不到,则从 Spring BeanDefinition 中获取,并**执行生命周期函数** ## 3. Spring 容器管理和游离对象 * 依赖对象 ![img](assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvbmdoYW8wMDc=,size_16,color_FFFFFF,t_70#pic_center-166528190061910.png) ## 4. Spring BeanDefinition 作为依赖来源 要素 * 元数据:BeanDefinition * 注册:BeanDefinitionRegistry#registerBeanDefinition * 类型:延迟和非延迟 * 顺序:Bean 生命周期顺序按照注册顺序 ## 5、外部化配置作为依赖来源 ![image-20221009171247256](assets/image-20221009171247256.png) # 面试题 1、注入和查找的来源是否相同? ![image-20221009172428729](assets/image-20221009172428729.png) 2、单例对象能在IOC容器启动后注册码? ![image-20221009172505792](assets/image-20221009172505792.png) 3、spring依赖注入的来源有哪些? ![image-20221009172913945](assets/image-20221009172913945.png)