Spring框架裡註解@Autowired的工作原理
Suppose I have a bean named HelloWorld which has a member attribute points to another bean User.
With annotation @Autowired, as long as getBean is called in the runtime, the returned HelloWorld instance will automatically have user attribute injected with User instance.
How is this behavior implemented by Spring framework?
(1) in Spring container implementation’s refresh method, all singleton beans will be initialized by default.
When the HelloWorld bean is initialized:
Since it has the following source code:
In the runtime, this annotation is available in metadata via reflection. In metadata structure below, the targetClass points to HelloWorld bean, and injectedElements points to the User class to be injected.
(2) In doResolveDependency, the definition for User bean is searched based on this.beanDefinitionNames ( list in DefaultListableBeanFactory ):
Once found, the found result is added to array candidateNames:
Then the constructor of User bean class is called ( still triggered by getBean call ), the user instance is created by calling constructor:
The created user instance together with its name “user” is inserted to the map matchingBeans.
- Finally the user reference is set to user attribute of HelloWorld instance via reflection. Here the variable bean in line 569 points to HelloWorld instance, and value points to user instance.
Once field.set(bean, value) is done, we can observe in debugger that the user attribute in HelloWorld instance is already injected successfully.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703494/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java JUnit框架裡@Category註解的工作原理Java框架Go
- Spring Ioc原始碼分析系列--@Autowired註解的實現原理Spring原始碼
- 死磕Spring之IoC篇 - @Autowired 等註解的實現原理Spring
- Spring裡component-scan的工作原理Spring
- JUnit 註解@RunWith的工作原理
- JUnit 註解@Category的工作原理Go
- JUnit 註解@SuiteClasses的工作原理UI
- JUnit 註解@Rule的工作原理
- spring 框架常用註解Spring框架
- 3.springboot-@Autowired和@Value工作原理Spring Boot
- Spring @Autowired 註解自動注入流程是怎麼樣?Spring
- Spring 框架快取註解Spring框架快取
- Angular @Injectable 註解的工作原理淺析Angular
- 如何實現一個簡易版的 Spring - 如何實現 @Autowired 註解Spring
- spring框架半自動註解Spring框架
- Angular單元測試框架裡API toHaveBeenCalledTimes的工作原理Angular框架API
- spring下應用@Resource, @Autowired 和 @Inject註解進行依賴注入的差異Spring依賴注入
- 《四 spring原始碼》spring的事務註解@Transactional 原理分析Spring原始碼
- 常用的自動裝配註解@Autowired @RequiredArgsConstructor @AllArgsConstructorUIStruct
- 聊聊依賴注入註解@Resource和@Autowired依賴注入
- Spring框架使用@Autowired自動裝配引發的討論Spring框架
- 深入學習Spring框架(二)- 註解配置Spring框架
- SAP Fiori @OData.publish 註解的工作原理解析
- spring-mvc的工作原理SpringMVC
- Spring Session工作原理SpringSession
- spring框架中三層架構相關的註解Spring框架架構
- 【Spring註解驅動開發】使用@Autowired@Qualifier@Primary三大註解自動裝配元件,你會了嗎?Spring元件
- SAP Fiori程式設計模型規範裡註解 - @OData.publish工作原理解析程式設計模型
- 使用Java JUnit框架裡的@Rule註解的用法舉例Java框架
- SAP Fiori 註解 @ObjectModel.readOnly工作原理解析Object
- SAP Fiori Elements 框架裡 Smart Table 控制元件的工作原理介紹框架控制元件
- Spring @Autowired 注入小技巧Spring
- spring boot使用@Async非同步註解,原理+原始碼Spring Boot非同步原始碼
- 【Java註解用法】@Autowired 與@Resource的區別以及@Qualifier的介紹Java
- Spring註解Spring
- Spring框架系列(10) - Spring AOP實現原理詳解之AOP代理的建立Spring框架
- 深入介紹 UI5 框架裡 Smart Field 控制元件的工作原理UI框架控制元件
- 死磕Spring之IoC篇 - @Bean 等註解的實現原理SpringBean