杀死Spring - BeanFactory系列中的AbstractBeanFactory
通过前面的博客我们知道AbstractBeanFactory在BeanFactory系列IOC容器中的位置为:
一:域
//父类IOC容器
private BeanFactory parentBeanFactory;
//bean的类加载器()
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
//这是个啥,临时类加载器?
private ClassLoader tempClassLoader;
//是否缓存bean的metadata()
private boolean cacheBeanMetadata = true;
//属性编辑登记员集合()
private final Set propertyEditorRegistrars = new LinkedHashSet(4);
//
private final Map customEditors = new HashMap(4);
//类型转化器()
private TypeConverter typeConverter;
//postProcessor集合()
private final List beanPostProcessors = new ArrayList();
//是否实例化AwareBeanPostProcessors()
private boolean hasInstantiationAwareBeanPostProcessors;
//是否构造AwareBeanPostProcessors()
private boolean hasDestructionAwareBeanPostProcessors;
//bean的scope集合(哪个bean是Singleton的哪个bean是Prototype的)
private final Map scopes = new HashMap();
//merged的BeanDefinition集合()
private final Map mergedBeanDefinitions = CollectionFactory.createConcurrentMapIfPossible(16);
//已经实例化的bean集合()
private final Set alreadyCreated = Collections.synchronizedSet(new HashSet());
//direct线程本地变量缓冲区()
private final ThreadLocal prototypesCurrentlyInCreation = new NamedThreadLocal("Prototype beans currently in creation");
二:方法
2.1:copyConfigurationFrom方法
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
Assert.notNull(otherFactory, "BeanFactory must not be null");
//
this.setBeanClassLoader(otherFactory.getBeanClassLoader());
//
this.setCacheBeanMetadata(otherFactory.isCacheBeanMetadata());
if (otherFactory instanceof AbstractBeanFactory) {
AbstractBeanFactory otherAbstractFactory = (AbstractBeanFactory)otherFactory;
this.customEditors.putAll(otherAbstractFactory.customEditors);
this.propertyEditorRegistrars.addAll(otherAbstractFactory.propertyEditorRegistrars);
this.beanPostProcessors.addAll(otherAbstractFactory.beanPostProcessors);
this.hasInstantiationAwareBeanPostProcessors = this.hasInstantiationAwareBeanPostProcessors || otherAbstractFactory.hasInstantiationAwareBeanPostProcessors;
this.hasDestructionAwareBeanPostProcessors = this.hasDestructionAwareBeanPostProcessors || otherAbstractFactory.hasDestructionAwareBeanPostProcessors;
this.scopes.putAll(otherAbstractFactory.scopes);
}
}
public void setBeanClassLoader(ClassLoader beanClassLoader) {
//若beanClassLoader为null就使用ClassUtils创建的
//ClassUtils.getDefaultClassLoader的逻辑是优先返回当前线程的上下文累加器,其次返回加载ClassUtils的类加载器
this.beanClassLoader = beanClassLoader != null ? beanClassLoader : ClassUtils.getDefaultClassLoader();
}
public void setCacheBeanMetadata(boolean cacheBeanMetadata) {
this.cacheBeanMetadata = cacheBeanMetadata;
}