大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Suppose I have a bean named HelloWorld which has a member attribute points to another bean User.
创新互联是一家业务范围包括IDC托管业务,虚拟空间、主机租用、主机托管,四川、重庆、广东电信服务器租用,成都二枢服务器租用托管,成都网通服务器托管,成都服务器租用,业务范围遍及中国大陆、港澳台以及欧美等多个国家及地区的互联网数据服务公司。
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.
(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:
@Autowiredprivate 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.
Once field.set(bean, value) is done, we can observe in debugger that the user attribute in HelloWorld instance is already injected successfully.
要获取更多Jerry的原创文章,请关注公众号"汪子熙":