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);
            }
        }
    
    
  • 相关阅读:
    《人件》读书笔记
    《人月神话》读书笔记
    使用表驱动编程设计打印万年历
    maven spring整合mybatis是使用junit测试报字节序列的错误
    idea中建立maven web项卡在Generating Project in Batch mode
    ideaIU-2016.2.5激活
    maven初步入门demo
    Scala基础篇-04 try表达式
    面试题12-旋转数组的最小值
    scala基础篇-03 if与for
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/5512565.html
Copyright © 2011-2022 走看看