@autowired 自动装配原理
时间: 2023-08-14 10:14:50 浏览: 96
回答: @Autowired是Spring提供的注解,用于实现自动装配。它的原理是通过IOC容器在扫描中扫描到@Autowired注解时,自动查找并装配所需的Bean对象。默认情况下,@Autowired按照类型进行装配,如果查询结果只有一个,就直接进行装配;如果查询结果有多个,@Autowired会根据名称进行查找并装配。\[2\]在启动Spring IOC时,容器会自动装配一个AutowiredAnnotationBeanPostProcessor后置处理器,用于处理@Autowired注解。当容器扫描到@Autowired注解时,后置处理器会在IOC容器中自动查找所需的Bean对象,并进行装配。\[3\]与@Autowired不同,@Resource是JDK提供的注解,也用于实现自动装配。@Resource默认按照名称进行装配,它会根据名称在IOC容器中查找所需的Bean对象进行装配。\[1\]总的来说,@Autowired和@Resource的区别在于注入方式和提供者不同。
#### 引用[.reference_title]
- *1* [@Autowired的自动装配原理](https://blog.csdn.net/qq_46853052/article/details/127502517)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [@Autowired自动装配及原理](https://blog.csdn.net/m0_59340907/article/details/127498517)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [@Autowired自动装配原理](https://blog.csdn.net/weixin_68286174/article/details/125943218)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文