日期:2014-05-20  浏览次数:20670 次

Spring Context start与web容器初始化的区别疑问?

使用ClassPathXmlApplicationContext的start方法与利用web容器去初始化配置BEAN有什么区别?

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

好像start做的事情会多点,请问多了哪些?

如果我要用容器初始化的话,如何操作相同事情?

------解决方案--------------------
Java code

    //---------------------------------
    // 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);
        }
    }

注意里面有一个递归。。开始生命周期。。

------解决方案--------------------
主要就是一个bean的加载过程,完成bean对象的初始化工作