循环依赖和bean的覆盖重写定义
源码里:
AbstractApplicationContext.java :
refresh()
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
refreshBeanFactory();
AbstractRefreshableApplicationContext.java:
refreshBeanFactory()
customizeBeanFactory(beanFactory);
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
if (this.allowBeanDefinitionOverriding != null) {
//默认为ture,允许覆盖bean的定义信息
beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
if (this.allowCircularReferences != null) {
//默认为true,允许循环引用
beanFactory.setAllowCircularReferences(this.allowCircularReferences);
}
}
可以对其已经定义好的信息进行重写定义:
继承ClassPathXmlApplicationContext 它可以重写AbstractRefreshableApplicationContext.java—》customizeBeanFactory(DefaultListableBeanFactory beanFactory)
public class A extends ClassPathXmlApplicationContext {
@Override
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
super.setAllowCircularReferences(false);
super.setAllowBeanDefinitionOverriding(false);
super.customizeBeanFactory(beanFactory);
}
}