美文网首页我爱编程
Spring MVC - ContextLoaderListen

Spring MVC - ContextLoaderListen

作者: 爱蛇 | 来源:发表于2018-05-24 23:08 被阅读0次

ContextLoaderListener

实际是上是ContextLoader的适配器(适配到ServletContextListener接口),
需要配置到web.xml里,才能进行IOC容器初始化。

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

ContextLoader

主要功能初始化ApplicationContext容器,默认是XmlWebApplicationContext类,其中有包含加载

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//通过servletContext.getAttribute 来判断是否已经进行过初始化
        if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
            throw new IllegalStateException(
                    "Cannot initialize context because there is already a root application context present - " +
                    "check whether you have multiple ContextLoader* definitions in your web.xml!");
        }

        Log logger = LogFactory.getLog(ContextLoader.class);
        servletContext.log("Initializing Spring root WebApplicationContext");
        if (logger.isInfoEnabled()) {
            logger.info("Root WebApplicationContext: initialization started");
        }
        long startTime = System.currentTimeMillis();

        try {
            // Store context in local instance variable, to guarantee that
            // it is available on ServletContext shutdown.
            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);
                    }
                //配置id, spring 配置文件路径 以及 自定义容器初始化即实现ApplicationContextInitializer接口的类
    configureAndRefreshWebApplicationContext(cwac, servletContext);
                }
            }
            servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

            ClassLoader ccl = Thread.currentThread().getContextClassLoader();
            if (ccl == ContextLoader.class.getClassLoader()) {
                currentContext = this.context;
            }
            else if (ccl != null) {
                currentContextPerThread.put(ccl, this.context);
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
                        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
            }
            if (logger.isInfoEnabled()) {
                long elapsedTime = System.currentTimeMillis() - startTime;
                logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
            }

            return this.context;
        }
        catch (RuntimeException ex) {
            logger.error("Context initialization failed", ex);
            servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
            throw ex;
        }
        catch (Error err) {
            logger.error("Context initialization failed", err);
            servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
            throw err;
        }
    }

XmlWebApplicationContext

ApplicationContext容器,主要用于初始化IOC容器里的bean
类架构如图:


XmlWebApplicationContext类架构图

可以看出实际他是一个ResouceLoader的一个默认实现类
DefaultResourceLoader的子类,所以XmlWebApplicatonContext拥有ResourceLoader的功能,能够加载不同的资源。

相关文章

网友评论

    本文标题:Spring MVC - ContextLoaderListen

    本文链接:https://www.haomeiwen.com/subject/icfucftx.html