zoukankan      html  css  js  c++  java
  • Spring 中context.start作用

    我们经常会看到 如下代码  

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configPath.split("[,\s]+"));
    context.start();

       其实该context.start()的作用就是初始话配置文件中的bean对象

        //---------------------------------------------------------------------
        // Implementation of Lifecycle interface
        //---------------------------------------------------------------------
    
        public void start() {
            Map lifecycleBeans = getLifecycleBeans();
            for (Iterator it = new LinkedHashSet(lifecycleBeans.keySet()).iterator(); it.hasNext();) {
                String beanName = (String) it.next();
                doStart(lifecycleBeans, beanName);
            }
            publishEvent(new ContextStartedEvent(this));
        }
    
        /**
         * Start the specified bean as part of the given set of Lifecycle beans,
         * making sure that any beans that it depends on are started first.
         * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
         * @param beanName the name of the bean to start
         */
        private void doStart(Map lifecycleBeans, String beanName) {
            Lifecycle bean = (Lifecycle) lifecycleBeans.get(beanName);
            if (bean != null) {
                String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName); //找到依赖的类
                for (int i = 0; i < dependenciesForBean.length; i++) {
                    doStart(lifecycleBeans, dependenciesForBean[i]);//递归调用找到依赖的类
                }
                if (!bean.isRunning()) {
                    bean.start();
                }
                lifecycleBeans.remove(beanName);
            }
        }
    
    
  • 相关阅读:
    Linux学习之探索文件系统
    `设计模式之代理模式
    `设计模式之单例模式
    `设计模式之工厂模式
    浅谈系列之 javascript原型与对象
    Javascript基础
    jQuery效率提升建议
    CSS基础知识汇总
    CSS-float详解,深入理解clear:both[转+部分原创]
    HTML基础知识汇总
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/5512565.html
Copyright © 2011-2022 走看看