一、基本使用
- 主要是三個注解的使用,@Autowired,@Value,@Resource
二、實現(xiàn)步驟
- 攔截bean的創(chuàng)建
要想攔截bean,就需要處理spring bean生命周期事件,spring通過一些接口來處理事件,實現(xiàn)屬性注入,需要 InstantiationAwareBeanPostProcessor接口來完成。 - 找到注解的字段,并注入值
獲取被注解bean,然后從bean容器中中尋找響應(yīng)類型的bean注入。
三、@Autowired,@Value是如何實現(xiàn)的
- 攔截bean的創(chuàng)建
- AutowiredAnnotationBeanPostProcessor實現(xiàn)了InstantiationAwareBeanPostProcessor接口
- spring-boot 在啟動的時候向容器中注入了AutowiredAnnotationBeanPostProcessor
- 找到注解的字段,并注入值
- 通過AutowiredAnnotationBeanPostProcessor.postProcessProperties()和AutowiredAnnotationBeanPostProcessor.postProcessMergedBeanDefinition()兩個方法完成
- 每一個需要依賴注入的bean都被封裝成了InjectionMetadata對象,每個要備注入的字段被封裝成了AutowiredFieldElement對象
三、@Resource是如何實現(xiàn)的
- 攔截bean的創(chuàng)建
- CommonAnnotationBeanPostProcessor實現(xiàn)了InstantiationAwareBeanPostProcessor接口
- spring-boot 在啟動的時候向容器中注入了CommonAnnotationBeanPostProcessor
- 找到注解的字段,并注入值
- CommonAnnotationBeanPostProcessor.postProcessProperties()和CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition()兩個方法完成
- 每一個需要依賴注入的bean都被封裝成了InjectionMetadata對象,每個要備注入的字段被封裝成了ResourceElement對象
文章來源地址http://www.zghlxwxcb.cn/news/detail-438805.html
文章來源:http://www.zghlxwxcb.cn/news/detail-438805.html
到了這里,關(guān)于spring-boot 依賴注入流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!