欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java注解@Autowired的工作原理 JavaSAPSAP云平台SAP Cloud PlatformSpring 

程序员文章站 2022-06-16 17:32:58
...

Suppose I have a bean named HelloWorld which has a member attribute points to another bean User.

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

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.

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

How is this behavior implemented by Spring framework? (1) in Spring container implementation’s refresh method, all singleton beans will be initialized by default.

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

When the HelloWorld bean is initialized:

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

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.

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

(2) In doResolveDependency, the definition for User bean is searched based on this.beanDefinitionNames ( list in DefaultListableBeanFactory ):

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

Once found, the found result is added to array candidateNames:

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

Then the constructor of User bean class is called ( still triggered by getBean call ), the user instance is created by calling constructor:

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

The created user instance together with its name “user” is inserted to the map matchingBeans.

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

(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.

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

Once field.set(bean, value) is done, we can observe in debugger that the user attribute in HelloWorld instance is already injected successfully.

 

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring 

 

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Java注解@Autowired的工作原理
            
    
    
        JavaSAPSAP云平台SAP Cloud PlatformSpring