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.
(3) 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的原创文章,请关注公众号"汪子熙":
相关推荐
通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合
Spring注解@Resource和@Autowired.doc
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
SSH笔记-通过@Autowired配置注入关系的demo,续上一篇文章中的annotation笔记
Spring@Autowired注解与自动装配.rar
Spring Ioc源码分析系列--@Autowired注解的实现原理.doc
自定义标签中@Autowired的属性为null 解决办法:两步 1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加 3.使用SpingContext.getBean("bean名");获取
【Java面试】@Resource 和 @Autowired 的区别.doc
主要介绍了详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
@Autowired使用 构造函数注入 public Class Outer { private Inner inner; @Autowired public Outer(Inner inner) { this.inner = inner; } } 属性注入 public Class Outer { @Autowired private Inner ...
Spring框架中 @Autowired 和 @Resource 注解的区别 在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。 首先,咱们简单了解 @...
主要介绍了Spring使用@Autowired为抽象父类注入依赖代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Intellij IDEA如何去掉@Autowired 注入警告的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了Spring注解@Resource和@Autowired区别对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.@Resource是按名称进行注入的,属于java自带的。@Autowired是按类型进行注入的,属于Spring。
主要介绍了详解SpringBoot 多线程处理任务 无法@Autowired注入bean问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了使用@Autowired注解警告Field injection is not recommended的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
3.这个程序具体的内容我忘了,只知道使用@Data@Configuration@Autowired@PropertySource@Test(指定自定义文件的位置,开启对应配置)等方法写的,具体实现的功能就是新建几个.yml和.java文件,实现在.yml文件中配置...
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。 1、共同点 两者都可以写在字段和setter方法上。两者...
@Autowired报Field injection is not recommended的解决办法 - 副本