字段
factoryBeanName
factoryMethodName
methodOverrides
propertyValues 属性 (从yaml或者properties中读取)
方法
setBeanClass
书接上文 要设置setBeanClass了
AnnotatedGenericBeanDefinition的构造方法
这里使用到了父类AbstractBeanDefinition的setBean方法
public void setBeanClass(@Nullable Class<?> beanClass) {
this.beanClass = beanClass;
}
beanClass在AbstractBeanDefinition是私有字段。
当一个类继承于另一个类,子类中没有父类的方法时。用子类的对象调用方法时,会首先在子类中查找,如果子类中没有改方法,再到父类中查找。当一个方法只在父类中定义时,调用该方法时会使用父类中的属性。
hasBeanClass()
public boolean hasBeanClass() {
return this.beanClass instanceof Class;
}
prepareMethodOverrides
overrideFrom
在之前的实例中出现这样一个现象合并BeanDefinition
child中明明并没有{“age”,“18”}这个PropertyValues,最终却呈现处理了,为什么?
首先看一下在执行方法之前的该RootBeanDefinition
我们都知道这时的BeanDefinition还只是父BeanDefinition,也就是RootBean所对应的BeanDefinition,ChildBeanDefinition还没有更新进去。
这一步的更新操作位置还是比较容易找到的
addPropertyValues是具有替换逻辑的,会将others的BeanDefinition(子BeanDefinition)与当前BeanDefinition重叠的地方更新为others的Property。有了这样的BeanDefinition,最终的实例Bean当然也会额外打印age:18
public void overrideFrom(BeanDefinition other) {
if (StringUtils.hasLength(other.getBeanClassName())) {
this.setBeanClassName(other.getBeanClassName());
}
if (StringUtils.hasLength(other.getScope())) {
this.setScope(other.getScope());
}
this.setAbstract(other.isAbstract());
if (StringUtils.hasLength(other.getFactoryBeanName())) {
this.setFactoryBeanName(other.getFactoryBeanName());
}
if (StringUtils.hasLength(other.getFactoryMethodName())) {
this.setFactoryMethodName(other.getFactoryMethodName());
}
this.setRole(other.getRole());
this.setSource(other.getSource());
this.copyAttributesFrom(other);
if (other instanceof AbstractBeanDefinition) {
AbstractBeanDefinition otherAbd = (AbstractBeanDefinition)other;
if (otherAbd.hasBeanClass()) {
this.setBeanClass(otherAbd.getBeanClass());
}
if (otherAbd.hasConstructorArgumentValues()) {
this.getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues());
}
if (otherAbd.hasPropertyValues()) {
this.getPropertyValues().addPropertyValues(other.getPropertyValues());
}
if (otherAbd.hasMethodOverrides()) {
this.getMethodOverrides().addOverrides(otherAbd.getMethodOverrides());
}
Boolean lazyInit = otherAbd.getLazyInit();
if (lazyInit != null) {
this.setLazyInit(lazyInit);
}
this.setAutowireMode(otherAbd.getAutowireMode());
this.setDependencyCheck(otherAbd.getDependencyCheck());
this.setDependsOn(otherAbd.getDependsOn());
this.setAutowireCandidate(otherAbd.isAutowireCandidate());
this.setPrimary(otherAbd.isPrimary());
this.copyQualifiersFrom(otherAbd);
this.setInstanceSupplier(otherAbd.getInstanceSupplier());
this.setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed());
this.setLenientConstructorResolution(otherAbd.isLenientConstructorResolution());
if (otherAbd.getInitMethodName() != null) {
this.setInitMethodName(otherAbd.getInitMethodName());
this.setEnforceInitMethod(otherAbd.isEnforceInitMethod());
}
if (otherAbd.getDestroyMethodName() != null) {
this.setDestroyMethodName(otherAbd.getDestroyMethodName());
this.setEnforceDestroyMethod(otherAbd.isEnforceDestroyMethod());
}
this.setSynthetic(otherAbd.isSynthetic());
this.setResource(otherAbd.getResource());
} else {
this.getConstructorArgumentValues().addArgumentValues(other.getConstructorArgumentValues());
this.getPropertyValues().addPropertyValues(other.getPropertyValues());
this.setLazyInit(other.isLazyInit());
this.setResourceDescription(other.getResourceDescription());
}
}