Spring源码-populateBean填充bean属性
一、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,
// 调用InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation,一般用于设置属性
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(); //工厂是否拥有InstiationAwareBeanPostProcessor
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);
}
}
-
调用InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation,一般用于设置属性。
-
获取自动装配模式,如果是按照名字或类型装配,则自动装配属性后添加到MutablePropertyValues
-
判断工厂是否拥有InstiationAwareBeanPostProcessor,如果有则工厂将给定的属性值应用到给定Bean之前,对它们进行后处理,即postProcessProperties,如果pvs为null且filteredPds为空过滤出需要依赖检查的属性,即filterPropertyDescriptorsForDependencyCheck,调用postProcessPropertyValues后置处理,一般进行检查是否所有依赖项都满足或者替换要应用的属性值
-
如果需要检查依赖,如果filteredPds为null则调用filterPropertyDescriptorsForDependencyCheck过滤要忽略检查的依赖属性,调用checkDependencies检查依赖。
-
将属性应用到bean