容器在加載BeanDefinition
時會調用bd.getPropertyValues().addPropertyValue(pv);
成員變量的定義信息添加到BeanDefinition
對象中java
容器在AbstractAutowireCapableBeanFactory
的doCreateBean()
方法中調用populate()
方法執行依賴注入的邏輯app
執行populate()
方法時會調用mbd.getPropertyValues()
獲取BeanDefinition
對象中的成員變量的定義信息,根據此信息進行依賴注入code
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { if (bw == null) { ... } ... // 獲取bean定義信息中的成員變量定義信息 PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null); if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } ... if (pvs != null) { // 應用成員變量的定義信息 applyPropertyValues(beanName, mbd, bw, pvs); } }
applyPropertyValues(beanName, mbd, bw, pvs);
——bw.setPropertyValues()
——setPropertyValue()
對象
遍歷成員變量定義信息,逐個注入get
容器根據成變量定義信息使用getBean()
獲取到bean,再使用反射調用setterXxx()
方法或其餘方法將bean注入到bean中it