【知识库】--spring ApplicationContext 加载配置文件 refresh()(241)

来源:互联网 发布:淘宝设置不包邮地区 编辑:程序博客网 时间:2024/06/13 17:26

ApplicationContext 扩展了 BeanFactory,大部分的企业应用使用ApplicationContext。


加载配置文件:

BeanFactory加载XML:BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactory.xml"));

ApplicationContext bf = new ClassPathXmlApplicationContext("beanFactory.xml");


public ClassPathXmlApplicationContext(String configLocation) throws BeansException{

           this.(new String[] {configLocation} , true ,null);

}


public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException{

          super(parent);

          setConfigLocations(configLocations);

         if(refresh){ refresh();}

}


1 设置配置路径:

public void setConfigLocations(String[] locations){

    if(locations != null) {

        Assert.noNullElements(locations,"Config locations must be null");//数组中不可有null

        this.configLoactions = new String[location.length];

        for(int i = 0; i < locations.length; I++){

               this.configLocations[i] = resolvePath(locations[i]).trim(); //解析给定的路径

        }

    }else{

       this.configLocations = null;

    }

}


2 扩展功能--refresh() 


public void refresh() throws BeansException, IllegalStateException{

          synchronized (this.startupShutdownMonitor){

                   //准备刷新的上下文环境  参考

                  prepareRefresh();

                  //tell the subclass to refresh the internal bean factory 初始化beanFactory,进行XML文件读取

                  ConfigurableListableBeanFactory beanFactory  = obtainFreshBeanFactory(); 参考

                 

                 //prepare the bean factory for use in this context 对BeanFactory 进行功能填充 @Qualifier @Autowired 注解在此增强

                 prepareBeanFactory(beanFactory);


                try{

                    //allows post-processing of the bean factory in context subclass 子类覆盖方法做额外的处理。可以在此进行业务相关的进一步扩展。

                    postProcessBeanFactory(beanFactory);


                   //激活各种BeanFactory处理器

                   invokeBeanFactoryPostProcessors(beanFactory);


                   //注册拦截Bean创建的Bean处理器,此处只是注册,真正的调用是在getBean时候

                   registerBeanPostProcessors(beanFactory);


                  //为上下文初始化Message源,国际化处理

                  initMessageSource();


                  //initialize event multicaster for this context 初始化应用消息广播器

                  initApplicationEventMulticaster();


                 //initialize other special beans in specific context subclasses 留给子类初始化其他的Bean

                 onRefresh();


                //check for listener beans and register them 在所有注册的bean中查找Listener Bean ,注册到消息广播中

                registerListeners();


                // instantiate all remaining (non-lazy-init) singletons 初始化剩下的单实例

                finishBeanFactoryInitialization(beanFactory);

              

                //最后一步 publish corresponding event

               //完成刷新过程,通知生命周期处理器 lifecycleProcessor 刷新完成,同时发出ContextRefreshEvent 通知别人

               finishRefresh();               

               }catch(BeanException ex){ destroyBeans(); cancelRefresh(ex); throw ex;}

          }

}












  

阅读全文
0 0
原创粉丝点击