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:
@Autowired private User user;
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.
本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
通过阶乘的例子,练习在JavaScript, Scala和ABAP里实现尾递归(Tail Recursion)
Before we start to research tail recursion, let’s first have a look at the normal recursion. A simple factorial implementation by recursion: function factorial(n){ if(n ===1) { return 1; } return n *factorial(n -1); } Let N = 5, see how new stack frame is created for each time of recursive call: We have two stack frames now, one stores the context when n = 5, and the topmost one for current calculation: n = 4 Now since n equals to 1, we stop recursion. The current stack frame ( n = 1 ) will be p...
- 下一篇
ABAP模拟Java Spring依赖注入(Dependency injection)的一个尝试
Recently I will deliver a session regarding dependency inversion principle to my team. As Java Spring is already widely used in all other Java development teams in my site, some ABAPers are not well aware of its idea and implementation under the hood. In order for ABAPers to easily understand the mechanism of Java Spring dependency inversion, I wrote a prototype in ABAP after going through related Java source code of Spring. Before I start, I perform the search in SCN. There are already several ...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7设置SWAP分区,小内存服务器的救世主
- Mario游戏-低调大师作品
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- 2048小游戏-低调大师作品
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7,CentOS8安装Elasticsearch6.8.6