1. 依赖查找的来源:除容器内建和自定义Spring Bean之外,还有其他来源提供依赖查找吗?
-
查找来源
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JSS4j7qU-1670835429210)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202104519610.png)]](https://img-blog.csdnimg.cn/51d8ec76a3ad4e16a9778d44ebf70e8c.png)
-
Spring 內建 BeanDefintion
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s1USFp7D-1670835429211)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202113551771.png)]](https://img-blog.csdnimg.cn/903a7c16da8742468e54933a99664e07.png)
-
Spring 內建单例对象
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hPyA3wXh-1670835429211)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202113628018.png)]](https://img-blog.csdnimg.cn/5c147e6a458f407587e5f76e06da8970.png)
-
当spring在注解环境下面, 这个 registerAnnotationConfigProcessors API会被调用, 它会被显示的去调用, 这个显示调用会有几个场景
第一个就是xml配置Annotation驱动
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/beans/spring-context.xsd "> <!-- 加载注解驱动所需要的 Spring 基础组件 --> <context:annotation-config /> <!-- 扫描 @Component Class --> <context:component-scan base-package="org.xiaoge" /> <!-- 实施加载bean--> <bean id="user" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.User"> <property name="id" value="1" /> <property name="name" value="xiaoge"/> <property name="city" value="WUHAN"/> <property name="configFileLocation" value="classpath:/META-INF/dependency-lookup-context.xml"/> <property name="workCities" value="WUHAN, HANGZHOU"/> <!-- <property name="lifeCities" value="WUHAN, BEIJING"/>--> <property name="lifeCities"> <list> <value>WUHAN</value> <value>BEIJING</value> </list> </property> <property name="map"> <map> <entry key="one" value="1" /> <entry key="two" value="2" /> </map> </property> </bean> <!-- 延迟bean 是沟通objectFactory简介创建的 --> <bean id="objectFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean"> <property name="targetBeanName" value="user" /> </bean> <!-- parent继承 --> <bean id="superUser" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.SuperUser" parent="user" primary="true"> <property name="address" value="武汉" /> </bean> </beans>annotation-config/component-scan都是注解驱动着一个XML元素的配置, 这些元素触发如下API调用:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CFcNKcM3-1670835429212)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202133824397.png)]](https://img-blog.csdnimg.cn/dd1739be08c74902819e51e211ae4ef5.png)
看一下它到底被谁给调用了
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5PhGQnW2-1670835429212)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202135008834.png)]](https://img-blog.csdnimg.cn/44bd4146a5a8437195b7e014456cc299.png)
它在xml处理的时候会被调用, 同时也会被, 同时ComponentScan里面进行调用了
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wi1Q5YGU-1670835429212)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202140644026.png)]](https://img-blog.csdnimg.cn/0f994c2727e04243a92741ad3c7e7365.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S2lLpvQh-1670835429213)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202140708742.png)]](https://img-blog.csdnimg.cn/099252a0c4ff41908901e373d61477b9.png)
包括AnnotatedBeanDefinitionReader它实际上是在AnnotationConfigApplicationContext关联的当它load的时候吧对应的信息读取进来
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jdSTcd3N-1670835429213)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202140805037.png)]](https://img-blog.csdnimg.cn/e649f11a2215426a9b13524417bea821.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1voSlbCX-1670835429213)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202141029841.png)]](https://img-blog.csdnimg.cn/3fd54942144245d081e5779f42dc0059.png)
最终调用 registerAnnotationConfigProcessors API把这些信息注入到应用上下文中, @Configuration @Autowired @Resource等
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dwhTj4OY-1670835429214)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202141307086.png)]](https://img-blog.csdnimg.cn/0952840f9058433a967e13f21bf3d7db.png)
这些Bean都会放到应用上下文中来进行注册, 这些东西都是称之为一个常规的Bean, 这些常规的Bean是在 AbstractApplicationContext#prepareBeanFactory 地方调用的, 这里头会注册非常多的一些信息, 包括上面的内建单例对象等
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-POWNoDYa-1670835429214)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202142117074.png)]](https://img-blog.csdnimg.cn/367df45aefde4f99949882bf357d8e4c.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lrEkWdum-1670835429214)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202142202778.png)]](https://img-blog.csdnimg.cn/e7920490b3a04f439064ade3cabe20ed.png)
那么当有BeanDefinition初始化完成之后, 它会调用 invokeBeanFactoryPostProcessors API, 通常我们AnnotationConfigUtils注册的这个类基本上BeanFactoryPostProcessor实现或者FactoryBean的实现, 因此在实现来说注册和调用它会激活它里面的生命周期。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y5RsZlip-1670835429215)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202142430160.png)]](https://img-blog.csdnimg.cn/8171bb05646d4824962b698db9465bcb.png)
2. 依赖注入的来源:难道依赖注入的来源与依赖查找的不同吗?
-
注入来源
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8f8ow2Pf-1670835429215)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202143127627.png)]](https://img-blog.csdnimg.cn/c651d1ba6b3e44118d8a079bce1de19d.png)
从这里可以看出它用了类型和对应做了个映射关系, 这种关系能够帮助我们进行依赖注入, 它这里还写了个相关的 API 它说这个地方是用于 autowiring 自动绑定时候所运用来找到, 那么这种方式也始终依赖注入的方式。那么相较于我们的依赖查找, 这里增加了种新的来源, 这几个对象在DefaultListableBeanFactory#findAutowireCandidates依赖查找中运用到
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ji314YO-1670835429215)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202144258718.png)]](https://img-blog.csdnimg.cn/1fe90559e33c40998ca0f5379ca09b11.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8YQMiVQE-1670835429216)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202145026305.png)]](https://img-blog.csdnimg.cn/e2fa61fda5044b8a95b7d64a3d630a59.png)
从this.resolvableDependencies值中可以看出我们锁保存的resolvableDependencies实际上就是我们刚刚注入或注册的由框架内部来进行注册的这些信息, 为了证明这个猜想回过头来看它里面的一个实现, 可以看到实际上就是这样做的, 它就是把东西方法ConcurrentHashMap里面去, 前面有四个注入的对象, 因此, 这个时候通过不同四个类型注入两个不同对象, 一个是BeanFactory, 一个是三个相同的List就是当前的应用上下文
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T84cHRlD-1670835429216)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202145951853.png)]](https://img-blog.csdnimg.cn/240debcf9f584d6bbd82490d3fcfe32d.png)
package org.xiaoge.thinking.in.spring.ioc.dependency.source; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.io.ResourceLoader; import javax.annotation.PostConstruct; /** * 依赖来源 示例 * * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a> * @since */ public class DependencySourceDemo { // 注入在 postProcessProperties 方法执行。早于 setter注入,也早于@PostConstruct @Autowired private BeanFactory beanFactory; @Autowired private ResourceLoader resourceLoader; @Autowired private ApplicationEventPublisher applicationEventPublisher; @Autowired private ApplicationContext applicationContext; @PostConstruct public void initByInjection() { System.out.println("beanFactory == applicationContext " + (beanFactory == applicationContext)); System.out.println("beanFactory == applicationContext.getBeanFactory() " + (beanFactory == applicationContext.getAutowireCapableBeanFactory())); System.out.println("resourceLoader == applicationContext " + (resourceLoader == applicationContext)); System.out.println("applicationEventPublisher == applicationContext " + (applicationEventPublisher == applicationContext)); } @PostConstruct public void initByLookup() { getBean(BeanFactory.class); getBean(ResourceLoader.class); getBean(ApplicationEventPublisher.class); getBean(ApplicationContext.class); } private <T> T getBean(Class<T> beanType) { try { return beanFactory.getBean(beanType); } catch (NoSuchBeanDefinitionException e) { System.err.println("当前类型" + beanType.getName() + "无法在 BeanFactory 中查找!"); } return null; } public static void main(String[] args) { // 创建 ApplicationContext 容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // 注册 Configuration Class 配置类 applicationContext.register(DependencySourceDemo.class); // 启动应用上下文 applicationContext.refresh(); // 关闭应用上下文 applicationContext.close(); } } // 运行结果 beanFactory == applicationContext false beanFactory == applicationContext.getBeanFactory() true resourceLoader == applicationContext true applicationEventPublisher == applicationContext true 当前类型org.springframework.beans.factory.BeanFactory无法在 BeanFactory 中查找! 当前类型org.springframework.core.io.ResourceLoader无法在 BeanFactory 中查找! 当前类型org.springframework.context.ApplicationEventPublisher无法在 BeanFactory 中查找! 当前类型org.springframework.context.ApplicationContext无法在 BeanFactory 中查找!总结:
Spring IOC的三种依赖来源,自定义注册的Spring bean、内建的Spring bean以及内建的可注入的依赖,其中自定义注册的Spring bean基本上是通过xml、注解或者api注册BeanDefination创建的,内建的Spring bean是通过registerSingleton()创建的,内建的可注入的依赖是通过registerResolveDependency()创建的,后续如果我们需要往Spring容器里放入一些非Spring托管的bean但又可以被依赖注入的, 可以通过registerResolveDependency() API实现
3. Spring容器管理和游离对象:为什么会有管理对象和游离对象?
-
依赖对象
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dbcuTmxT-1670835429216)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202160552393.png)]](https://img-blog.csdnimg.cn/861f5f5d2a464e7ab8d763ce109d6776.png)
4. Spring Bean Definition作为依赖来源:Spring Bean的来源
-
要素
-
元数据:BeanDefinition
-
注册:BeanDefinitionRegistry#registerBeanDefinition
-
类型:延迟和非延迟
-
顺序:Bean 生命周期顺序按照注册顺序
BeanDefinitionRegistry它是个注册中心,它里面包含 增/删/查 没有修改, 修改我们可以通过生命周期postProcessMergedBeanDefinition这么一个回调方法来进行操作
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-opRGb1Tf-1670835429217)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202164457864.png)]](https://img-blog.csdnimg.cn/378515ed8f624907b14c6220edde46f1.png)
BeanDefinitionRegistry它的一个标准的实现我们只需要看DefaultListableBeanFactory这个类
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I0uDTl4p-1670835429217)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202165209832.png)]](https://img-blog.csdnimg.cn/470ea6b10a224349a51cdf3cafb46d2f.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lhuZSrAc-1670835429217)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202170503877.png)]](https://img-blog.csdnimg.cn/8cd15fe98b974c5585218c5e30f3ef62.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T8AgYHmw-1670835429218)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202170622294.png)]](https://img-blog.csdnimg.cn/7518b382596044969b280bb5d5086867.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nMpbtaLC-1670835429218)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221202175056199.png)]](https://img-blog.csdnimg.cn/be4c7c38aa6745e7af47ac3d5c004b94.png)
-
5. 单例对象作为依赖来源:单体对象与普通Spring Bean存在哪些差异?
-
要素
- 来源:外部普通 Java 对象(不一定是 POJO )
- 注册:SingletonBeanRegistry#registerSingleton
-
限制
- 无生命周期管理
- 无法实现延迟初始化 Bean
因为它是一个单例对象, 也是来源于外部, 因此它的生命周期不由spring上下文来进行托管, 第二方面它没有办法实现所有对的延迟初始化, 道理非常简单, 由于单一对象没有在 Spring Ioc 容器里面进行托管, 所以它没有办法执行我们的生命周期管理, 同理可得, 它的延迟加载也是没有办法进行处理的, 所以这里是它的一个限制, 但是它可以用于依赖查找, 也可以用于依赖注入
注册
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zdWCA9Xu-1670835429218)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212143820125.png)]](https://img-blog.csdnimg.cn/f3a9c70e703443c7927130d1501d7411.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lyKECEkH-1670835429219)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212143847816.png)]](https://img-blog.csdnimg.cn/1c7257e960174a5b8e2916372f3e5672.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gPvVEJQQ-1670835429219)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212143922712.png)]](https://img-blog.csdnimg.cn/3e0756de1ace4eec99a7e568af5066b2.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WpQFh35i-1670835429219)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212144949531.png)]](https://img-blog.csdnimg.cn/1192381182234a74b94941c7f88d099e.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vijr1wM5-1670835429220)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145005479.png)]](https://img-blog.csdnimg.cn/fcc9af8079fd43d0a23cb4283391644e.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hfc4DCt2-1670835429220)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145051778.png)]](https://img-blog.csdnimg.cn/e0360528976e49328f6daa6e0ded7b69.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9S6nTkDH-1670835429220)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145110175.png)]](https://img-blog.csdnimg.cn/2dcbd5e55fc641ceacb9be04d7a499a3.png)
依赖查找
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b1R85epU-1670835429221)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145256899.png)]](https://img-blog.csdnimg.cn/ebad1a1fc21146959c0bd49c8c561c20.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aryflvs4-1670835429221)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145527668.png)]](https://img-blog.csdnimg.cn/481b507faa854d5bb952737e5748fd7d.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PWiPSNjt-1670835429221)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145546396.png)]](https://img-blog.csdnimg.cn/bbe9b1d752094a99bff56ac94371c7c0.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MpxE7TTL-1670835429222)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145642437.png)]](https://img-blog.csdnimg.cn/9c7e83930c6b4b57a309f06f424e264e.png)
而不是走的 else BeanDefinition 那条路, BeanDefinition 相对来说是比较复杂的, 因为它查找 BeanDefinition 之后, 它需要把BeanDefinition 变成 Bean 那么就会激活所有 Bean 的生命周期, 那之后就会有个生命周期管理, 而 SingletonBean 它其实比较简单只把对象直接返回就行, 所以相对来说它的方式比较单一一点
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0YtZAg7P-1670835429222)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212145742828.png)]](https://img-blog.csdnimg.cn/84acc1ac388442659deb7c981ddc39e1.png)
总结:
有人就会问了为什么要采用BeanDefinition的方式在存储元数据,每次在getBean的时候都需要初始化bean,直接采用singletonObjects方式存储不是更简单吗? 因为BeanDefinition对象有完整的生命周期控制,比如初始化等,更便于你业务bean的扩展(就是为了提高框架的扩展点,便于自己的定制化操作)
6. 非Spring容器管理对象作为依赖来源:如何理解ResolvableDependency?
-
要素
- 注册:ConfigurableListableBeanFactory#registerResolvableDependency
-
限制
- 无生命周期管理
- 无法实现延迟初始化 Bean
- 无法通过依赖查找
package org.xiaoge.thinking.in.spring.ioc.dependency.source; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import javax.annotation.PostConstruct; /** * ResolvableDependency 作为依赖来源 * * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a> * @since */ public class ResolvableDependencysourceDemo { @Autowired private String value; @PostConstruct public void init() { System.out.println(value); } public static void main(String[] args) { // 创建 ApplicationContext 容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // 注册 Configuration Class 配置类 applicationContext.register(ResolvableDependencysourceDemo.class); // 回调, 它是在初始化之前回调 applicationContext.addBeanFactoryPostProcessor(beanFactory -> { // 注册 Resolvable Dependency beanFactory.registerResolvableDependency(String.class, "Hello world"); }); // 启动应用上下文 applicationContext.refresh(); // 关闭应用上下文 applicationContext.close(); } } // 运行结果 Hello world
7. 外部化配置作为依赖来源:@Value是如何将外部化配置注入Spring Bean的?
-
要素
- 类型:非常规 Spring 对象依赖来源
-
限制
- 无生命周期管理
- 无法实现延迟初始化 Bean
- 无法通过依赖查找
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bYNg8ply-1670835429222)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212162252165.png)]](https://img-blog.csdnimg.cn/6e723541a9d24abeb2c57c2af952e944.png)
package org.xiaoge.thinking.in.spring.ioc.dependency.source; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.io.Resource; /** * 外部配置作为依赖来源 * * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a> * @since */ @Configuration @PropertySource(value = "META-INF/default.properties", encoding = "UTF-8") // 加载配置文件注解 public class ExternalConfiqurationDependencySourceDemo { @Value("${user.id:-1}") private Long id; @Value("${usr.name:xiaoge}") private String name; @Value("${user.resource:META-INF/default.properties}") private Resource resource; public static void main(String[] args) { // 创建 ApplicationContext 容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // 注册 Configuration Class 配置类 applicationContext.register(ExternalConfiqurationDependencySourceDemo.class); // 启动应用上下文 applicationContext.refresh(); ExternalConfiqurationDependencySourceDemo demo = applicationContext.getBean(ExternalConfiqurationDependencySourceDemo.class); System.out.println("demo.id = " + demo.id); System.out.println("demo.name = " + demo.name); System.out.println("demo.resource = " + demo.resource); // 关闭应用上下文 applicationContext.close(); } } // 运行结果 demo.id = 1 demo.name = 啸哥 demo.resource = class path resource [META-INF/default.properties]@Value的实现
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JquNxhMX-1670835429223)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212162401208.png)]](https://img-blog.csdnimg.cn/645104c7a1784734979de897aa3d8594.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9PEpNDJq-1670835429223)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212162615904.png)]](https://img-blog.csdnimg.cn/d55513dc2b0d454c9438a41d29756c39.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4kfq2vvb-1670835429223)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212162631248.png)]](https://img-blog.csdnimg.cn/f53315663adb4a5bbfbcde6675d3523c.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QhgF2gKy-1670835429223)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212162708313.png)]](https://img-blog.csdnimg.cn/d284eb957911466a83dd86c4f17579e7.png)

从图中可以看出他可以操作@Value和@Qualifier直接
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yUqSs15p-1670835429224)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212163055679.png)]](https://img-blog.csdnimg.cn/4e54dac41b384fa89450f3f20372f578.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UfFX9EER-1670835429224)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212162805963.png)]](https://img-blog.csdnimg.cn/1010500b598040f9a625fbe141730553.png)
strVal是从resolveEmbeddedValue去出来的
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NyydLr1O-1670835429225)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221212163421531.png)]](https://img-blog.csdnimg.cn/174bd502208f47e49322ab0861b95176.png)
8. 面试题精选
- 注入和查找的依赖来源是否相同?
- 答:否,依赖查找的来源仅限于 Spring BeanDefinition 以及单例 对象,而依赖注入的来源还包括 Resolvable Dependency 以及 @Value 所标注的外部化配置
- 单例对象能在 IoC 容器启动后注册吗?
- 答:可以的,单例对象的注册与 BeanDefinition 不同,BeanDefinition 会被 ConfigurableListableBeanFactory#freezeConfiguration() 方法影响,从而冻结注册,单例对象则没有这个限制。
- Spring 依赖注入的来源有哪些?
- 答:
- Spring BeanDefinition
- 单例对象
- Resolvable Dependency
- @Value 外部化配置
- 答:







![[附源码]Python计算机毕业设计钓鱼爱好者交流平台Django(程序+LW)](https://img-blog.csdnimg.cn/8b5f7018758f43cfb74d03492e8069b7.png)











