跳到主要内容

吃透Spring源码(二):customizeBeanFactory方法扩展点

> Spring的强大之处不仅仅在于它为Java开发者提供了极大便利,更在于它的开放式架构,使得用户可以拥有最大扩展Spring的能力。

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {


// 如果属性allowBeanDefinitionOverriding不为空,设置给beanFactory对象相应属性,是否允许覆盖同名称的不同定义的对象
if (this.allowBeanDefinitionOverriding != null) {


beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// 如果属性allowCircularReferences不为空,设置给beanFactory对象相应属性,是否允许bean之间存在循环依赖
if (this.allowCircularReferences != null) {


beanFactory.setAllowCircularReferences(this.allowCircularReferences);
}
}

在AbstractRefreshableApplicationContext类中有一个customizeBeanFactory方法是留给子类扩展,它是在refresh()的第二个方法obtainFreshBeanFactory()–>refreshBeanFactory()方法中调用。

protected final void refreshBeanFactory() throws BeansException {


// 如果存在beanFactory,则销毁beanFactory
if (hasBeanFactory()) {


destroyBeans();
closeBeanFactory();
}
try {


// 创建DefaultListableBeanFactory对象
DefaultListableBeanFactory beanFactory = createBeanFactory();
// 为了序列化指定id,可以从id反序列化到beanFactory对象
beanFactory.setSerializationId(getId());
// 定制beanFactory,设置相关属性,包括是否允许覆盖同名称的不同定义的对象以及循环依赖
customizeBeanFactory(beanFactory);
// 初始化documentReader,并进行XML文件读取及解析,默认命名空间的解析,自定义标签的解析
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
catch (IOException ex) {


throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}