首页 > 编程语言 >Spring源码-populateBean填充bean属性

Spring源码-populateBean填充bean属性

时间:2022-10-01 10:11:40浏览次数:57  
标签:populateBean pvs AUTOWIRE mbd Spring beanName 源码 bw null

一、bean属性注入模式
AutowireCapableBeanFactory

/**
 * 没有自动装配
 */
int AUTOWIRE_NO = 0;

/**
 * 按照名字自动装配
 */
int AUTOWIRE_BY_NAME = 1;

/**
 * 按照类型自动装配
 */
int AUTOWIRE_BY_TYPE = 2;

/**
 * 按照构造器自动装配
 */
int AUTOWIRE_CONSTRUCTOR = 3;

/**
 * 自动检测
 */
@Deprecated
int AUTOWIRE_AUTODETECT = 4;

二、populateBean

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.
	if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
		for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
			if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
				return;
			}
		}
	}

	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(beanName, mbd, bw, newPvs);
		}
		pvs = newPvs;
	}

	boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
	boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

	PropertyDescriptor[] filteredPds = null;
	if (hasInstAwareBpps) {
		if (pvs == null) {
			pvs = mbd.getPropertyValues();
		}
		for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
			PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
			if (pvsToUse == null) {
				if (filteredPds == null) {
					filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
				}
				pvsToUse = bp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
				if (pvsToUse == null) {
					return;
				}
			}
			pvs = pvsToUse;
		}
	}
	if (needsDepCheck) {
		if (filteredPds == null) {
			filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
		}
		checkDependencies(beanName, mbd, filteredPds, pvs);
	}

	if (pvs != null) {
		applyPropertyValues(beanName, mbd, bw, pvs);
	}
}

标签:populateBean,pvs,AUTOWIRE,mbd,Spring,beanName,源码,bw,null
From: https://www.cnblogs.com/shigongp/p/16746833.html

相关文章

  • 启动 Hello Spring Security Boot 应用
    本文章对如何快速启动一个启动HelloSpringSecurityBoot应用进行说明。下载代码在这个项目中,使用的是 spring.io 的项目生成程序,生成的地址为:https://start.sprin......
  • Spring
    Spring概念Spring框架概述Spring是轻量级的开源的EE框架轻量:依赖需求少,体积小,独立使用开源:提供源码框架Spring可以解决企业开发的复杂性两个核心:IOC和AOP......
  • springboot自动配置原理以及手动实现配置类
    springboot自动配置原理以及手动实现配置类1、原理spring有一个思想是“约定大于配置”。配置类自动配置可以帮助开发人员更加专注于业务逻辑开发,springboot在启动的时候......
  • springboot自动配置原理以及手动实现配置类
    springboot自动配置原理以及手动实现配置类1、原理spring有一个思想是“约定大于配置”。配置类自动配置可以帮助开发人员更加专注于业务逻辑开发,springboot在启动的时......
  • Spring源码-AbstractAutowireCapableBeanFactory的instantiateBean无参构造实例化bean
    instantiateBeanprotectedBeanWrapperinstantiateBean(StringbeanName,RootBeanDefinitionmbd){ try{ ObjectbeanInstance; if(System.getSecurityManager(......
  • Spring源码-AbstractAutowireCapableBeanFactory的autowireConstructor
    autowireConstructor:protectedBeanWrapperautowireConstructor( StringbeanName,RootBeanDefinitionmbd,@NullableConstructor<?>[]ctors,@NullableObject[......
  • 源码学习之MyBatis的底层查询原理
    导读本文通过MyBatis一个低版本的bug(3.4.5之前的版本)入手,分析MyBatis的一次完整的查询流程,从配置文件的解析到一个查询的完整执行过程详细解读MyBatis的一次查询流程,通过本......
  • Spring Security 在 Servlet 的作用区域
    SpringSecurity使用标准的Servlet 过滤器(Filter) 并与Servlet容器集成。这个意味着SpringSecurity可以在任何运行运行在Servlet容器(ServletContainer)中的应用......
  • 0044-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/bytes目标Buf是一个trait,里面有几个方法需要实现,Bytes实现了Buf。re......
  • 0045-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://docs.rs/bytes/latest/bytes/struct.Bytes.html目标Bytes的源码基本上看完了,简单使用一下其中......