Spring源码--两种启动方式

一. 通过编码方式启动Spring

1. 通过FileSystemXmlApplicationContext启动spring容器

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

        super(parent);
        setConfigLocations(configLocations);
        if (refresh) {
            refresh();
        }
    }
 

2.通过ClassPathXmlApplicationContext启动spring容器

public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent)
            throws BeansException {

        super(parent);
        Assert.notNull(paths, "Path array must not be null");
        Assert.notNull(clazz, "Class argument must not be null");
        this.configResources = new Resource[paths.length];
        for (int i = 0; i < paths.length; i++) {
            this.configResources[i] = new ClassPathResource(paths[i], clazz);
        }
        refresh();
    }

二. 通过Web容器方式启动Spring   

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {        

       /**
         * Initialize the root web application context.
         */
         @Override
         public void contextInitialized(ServletContextEvent event) {
               initWebApplicationContext(event.getServletContext());
         }
         /**
           * Close the root web application context.
           */
        @Override
        public void contextDestroyed(ServletContextEvent event) {
             closeWebApplicationContext(event.getServletContext());
             ContextCleanupListener.cleanupAttributes(event.getServletContext());
        }

   }

       ServletContextListener 接口是Web容器暴露出来的接口,ContextLoaderListener 是它的一个实现。Web容器在启动时会去调用它的contextInitialized方法从而实现spring容器的启动。

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {

           if (this.context == null) {
                this.context = createWebApplicationContext(servletContext);
            }
            if (this.context instanceof ConfigurableWebApplicationContext) {
                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
                if (!cwac.isActive()) {
                    // The context has not yet been refreshed -> provide services such as
                    // setting the parent context, setting the application context id, etc
                    if (cwac.getParent() == null) {
                        // The context instance was injected without an explicit parent ->
                        // determine parent for root web application context, if any.
                        ApplicationContext parent = loadParentContext(servletContext);
                        cwac.setParent(parent);
                    }
                    configureAndRefreshWebApplicationContext(cwac, servletContext);
                }
            }

     }

   initWebApplicationContext方法的具体实现在父类ContextLoader类中。重点关注configureAndRefreshWebApplicationContext()方法。需要注意此处的cwac 默认情况下是XmlWebApplicationContext的实例,它是ConfigurableWebApplicationContext 的实现类。

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {

       ConfigurableEnvironment env = wac.getEnvironment();
        if (env instanceof ConfigurableWebEnvironment) {
            ((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
        }

        customizeContext(sc, wac);
        wac.refresh();

}

  可以看到,最后都有一个refresh()方法。此方法在AbstractApplicationContext类中有具体实现,然后在refresh()方法完成了对spring容器的启动。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值