spring初始化隨筆

spring的初始化入口,是一個名叫ContextLoaderListener的監聽器類,實現了ServletContextListener接口,繼承ContextLoader類

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
    public ContextLoaderListener() {
    }
     ...

    public void contextInitialized(ServletContextEvent event) {
        this.initWebApplicationContext(event.getServletContext());
    }
    ....
}

其中,初始化的方法就是contextInitialize,此方法實現自ServletContextListener。ServletContextListener中有兩個接口,一個用於初始化容器使用,一個用於容器關閉時使用:

public interface ServletContextListener extends EventListener {
    default void contextInitialized(ServletContextEvent sce) {
    }

    default void contextDestroyed(ServletContextEvent sce) {
    }
}

接着看具體的實現方法initWebApplicationContext方法,此處調用的是父類ContextLoader中的方法:

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
    // 判斷容器ServletContext中如果已經創建了WebApplicationContext對象,如果有,就拋出異常
    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!");
    } else {
       ....

        try {

            if(this.context == null) {
                // 創建WebApplicationContext對象,即初始化
                this.context = this.createWebApplicationContext(servletContext);
            }

            if(this.context instanceof ConfigurableWebApplicationContext) {
                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)this.context;
                if(!cwac.isActive()) {
                    if(cwac.getParent() == null) {
                        ApplicationContext parent = this.loadParentContext(servletContext);
                        cwac.setParent(parent);
                    }

                    this.configureAndRefreshWebApplicationContext(cwac, servletContext);
                }
            }

           // 將WebApplicationContext以ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE名字存儲到servletContext中
           // String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT"; 

           servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
           ...

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

可以在跟蹤創建webApplicationContext的方法,如下

protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
    Class<?> contextClass = this.determineContextClass(sc);
    if(!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
        throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
    } else {
        return (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);
    }
}

protected Class<?> determineContextClass(ServletContext servletContext) {
    String contextClassName = servletContext.getInitParameter("contextClass");
    if(contextClassName != null) {
       ....
    } else {
        contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());

       ...
        }
    }
}

defaultStrategies就是讀取文件的流對象

static {
    try {
        ClassPathResource resource = new ClassPathResource("ContextLoader.properties", ContextLoader.class);
        defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
    } catch (IOException var1) {
        throw new IllegalStateException("Could not load 'ContextLoader.properties': " + var1.getMessage());
    }

    currentContextPerThread = new ConcurrentHashMap(1);
}

其中,ContextLoader.properties如下內容org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

通過子類XmlWebApplicationContext進行反射初始化WebApplicationContext

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章