AbstractAutoWireCapableBeanFactory 对Bean 实例进行属性依赖注入
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                AbstractAutoWireCapableBeanFactory 对Bean 实例进行属性依赖注入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                應用第一次通過getBean()方法(配置了lazy-init 預實例化屬性的除外)向IOC 容器索取Bean 時,容器創建Bean 實例對象, 并且對Bean 實例對象進行屬性依賴注入,AbstractAutoWireCapableBeanFactory 的populateBean()方法就是實現Bean 屬性依賴注入的功能,其主要源碼如下:
//將Bean屬性設置到生成的實例對象上 protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {if (bw == null) {if (mbd.hasPropertyValues()) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");}else {// Skip property population phase for null instance.return;}}// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the// state of the bean before properties are set. This can be used, for example,// to support styles of field injection.boolean continueWithPropertyPopulation = true;if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (BeanPostProcessor bp : getBeanPostProcessors()) {if (bp instanceof InstantiationAwareBeanPostProcessor) {InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {continueWithPropertyPopulation = false;break;}}}}if (!continueWithPropertyPopulation) {return;}//獲取容器在解析Bean定義資源時為BeanDefiniton中設置的屬性值PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);//對依賴注入處理,首先處理autowiring自動裝配的依賴注入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.//根據Bean名稱進行autowiring自動裝配處理if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {autowireByName(beanName, mbd, bw, newPvs);}// Add property values based on autowire by type if applicable.//根據Bean類型進行autowiring自動裝配處理if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {autowireByType(beanName, mbd, bw, newPvs);}pvs = newPvs;}//對非autowiring的屬性進行依賴注入處理boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);if (hasInstAwareBpps || needsDepCheck) {if (pvs == null) {pvs = mbd.getPropertyValues();}PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);if (hasInstAwareBpps) {for (BeanPostProcessor bp : getBeanPostProcessors()) {if (bp instanceof InstantiationAwareBeanPostProcessor) {InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);if (pvs == null) {return;}}}}if (needsDepCheck) {checkDependencies(beanName, mbd, filteredPds, pvs);}}if (pvs != null) {//對屬性進行注入applyPropertyValues(beanName, mbd, bw, pvs);} }?
總結
以上是生活随笔為你收集整理的AbstractAutoWireCapableBeanFactory 对Bean 实例进行属性依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 再述autowiring
- 下一篇: Spring IOC 容器根据Bean
