SpringloC容器的依赖注入源码解析(9)—— populateBean

本文详细探讨了Spring容器如何在populateBean方法中进行依赖注入,包括调用Setter方法、类型转换、处理集合类型以及自动装配逻辑。通过分析InstantiationAwareBeanPostProcessors后置处理器的责任链模式,以及MutablePropertyValues类在属性注入过程中的作用,揭示了Spring如何智能地管理和装配bean的属性。
摘要由CSDN通过智能技术生成

站在设计者的角度设计populateBean:

  • 调用Bean的Setter方法实例去给Bean设置上属性值

  • 变量类型的转换,同时还要考虑处理集合类型的情况

    • 配置的时候都是以字符串的形式来配置的
  • 处理显式自动装配的逻辑(autowire = byName或byType)

请添加图片描述

用两个类来做测试,GirlFriend类中注入了BoyFriend的实例,BoyFriend中注入了自己的实例:

package com.wjw.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BoyFriend {
   
   @Autowired
   private BoyFriend boyFriend;
}

package com.wjw.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class GirlFriend {
   
   @Autowired
   private BoyFriend boyFriend;
}

从AbstractAutowireCapableBeanFactory的doCreateBean方法中进入

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;
					}
				}
			}
		}

		// 如果上面设置continueWithPropertyPopulation = false,表明用户可能已经自己填充了
		// bean的属性,不需要Spring帮忙填充了。此时直接返回即可
		if (!continueWithPropertyPopulation) {
   
			return;
		}
		// pvs是一个MutablePropertyValues实例,里面实现了PropertyValues接口,
		// 提供属性的读写操作实现,同时可以通过调用构造函数实现深拷贝
		// 获取BeanDefinition里面为Bean设置上的属性值
		PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);

		int resolvedAutowireMode = mbd.getResolvedAutowireMode();
		if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
   
			MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
			// Add property values based on autowire by name if applicable.
			if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
   
				autowireByName(beanName, mbd, bw, newPvs);
			}
			// Add property values based on autowire by type if applicable.
			if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
   
				autowireByType
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王曾是少年

如果对你有帮助,欢迎支持我

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值