Spring源码分析--AbstractRefreshableApplicationContext(七)

来源:互联网 发布:php curl 断点续传 编辑:程序博客网 时间:2024/06/08 16:44

ApplicationContext应用上下文体系:

AbstractRefreshableApplicationContext主要是和容器的刷新与创建有关,调用refreshBeanFactory方法完成applicationContext.xml的解析操作,完成容器的初始化操作。

//类似于初始化容器,这里的容器就是DefaultListableBeanFactory@Overrideprotected final void refreshBeanFactory() throws BeansException {//如果已经有容器,销毁容器中的bean,关闭容器 if (hasBeanFactory()) {destroyBeans();closeBeanFactory();}try {//创建IoC容器 DefaultListableBeanFactory beanFactory = createBeanFactory();beanFactory.setSerializationId(getId());//对IoC容器进行定制化,如设置启动参数,开启注解的自动装配等customizeBeanFactory(beanFactory);//调用载入Bean定义的方法,主要这里又使用了一个委派模式,在当前类中只定义了抽象的loadBeanDefinitions方法,具体的实现调用子类容器loadBeanDefinitions(beanFactory);synchronized (this.beanFactoryMonitor) {this.beanFactory = beanFactory;}}catch (IOException ex) {throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);}}
AbstractRefreshableApplicationContext也提供了一些容器操作的方法,完整的源码如下:

/** * 主要是和容器刷新和创建相关 */public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {private Boolean allowBeanDefinitionOverriding;private Boolean allowCircularReferences;private DefaultListableBeanFactory beanFactory;private final Object beanFactoryMonitor = new Object();public AbstractRefreshableApplicationContext() {}public AbstractRefreshableApplicationContext(ApplicationContext parent) {super(parent);}public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {this.allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;}public void setAllowCircularReferences(boolean allowCircularReferences) {this.allowCircularReferences = allowCircularReferences;}//类似于初始化容器,这里的容器就是DefaultListableBeanFactory@Overrideprotected final void refreshBeanFactory() throws BeansException {//如果已经有容器,销毁容器中的bean,关闭容器 if (hasBeanFactory()) {destroyBeans();closeBeanFactory();}try {//创建IoC容器 DefaultListableBeanFactory beanFactory = createBeanFactory();beanFactory.setSerializationId(getId());//对IoC容器进行定制化,如设置启动参数,开启注解的自动装配等customizeBeanFactory(beanFactory);//调用载入Bean定义的方法,主要这里又使用了一个委派模式,在当前类中只定义了抽象的loadBeanDefinitions方法,具体的实现调用子类容器loadBeanDefinitions(beanFactory);synchronized (this.beanFactoryMonitor) {this.beanFactory = beanFactory;}}catch (IOException ex) {throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);}}//取消刷新@Overrideprotected void cancelRefresh(BeansException ex) {synchronized (this.beanFactoryMonitor) {if (this.beanFactory != null)this.beanFactory.setSerializationId(null);}super.cancelRefresh(ex);}//销毁工厂@Overrideprotected final void closeBeanFactory() {synchronized (this.beanFactoryMonitor) {this.beanFactory.setSerializationId(null);this.beanFactory = null;}}//判断Bean工厂是否还在protected final boolean hasBeanFactory() {synchronized (this.beanFactoryMonitor) {return (this.beanFactory != null);}}//获取bean工厂@Overridepublic final ConfigurableListableBeanFactory getBeanFactory() {synchronized (this.beanFactoryMonitor) {if (this.beanFactory == null) {throw new IllegalStateException("BeanFactory not initialized or already closed - " +"call 'refresh' before accessing beans via the ApplicationContext");}return this.beanFactory;}}@Overrideprotected void assertBeanFactoryActive() {}//创建一个新的bean工厂protected DefaultListableBeanFactory createBeanFactory() {return new DefaultListableBeanFactory(getInternalParentBeanFactory());}protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {if (this.allowBeanDefinitionOverriding != null) {beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);}if (this.allowCircularReferences != null) {beanFactory.setAllowCircularReferences(this.allowCircularReferences);}}//读取applicationContext.xml中的配置protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)throws BeansException, IOException;}



0 0