博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javaweb应用初始化spring applicationContext过程
阅读量:4230 次
发布时间:2019-05-26

本文共 4957 字,大约阅读时间需要 16 分钟。

通过ContextLoaderListener初始化applicationContext

javaweb引用启动时会加载web.xml,并且会执行配置的listener,在这里注册一个监听器,在内部初始化applicationContext就可以了。这里监听的类是org.springframework.web.context.ContextLoaderListener

该类继承了ServletContextListener接口,web应用启动时会执行contextInitialized方法。

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

可以看到,该方法就是用来初始化applicationContext的。

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {        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 {            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 {                if(this.context == null) {                    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);                    }                }                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 var8) {                logger.error("Context initialization failed", var8);                servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, var8);                throw var8;            } catch (Error var9) {                logger.error("Context initialization failed", var9);                servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, var9);                throw var9;            }        }    }

继续打开往下看,applicationContext是存在servletContext中的,key值为ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的值,可以看到是通过调用

createWebApplicationContext初始化的,内部调用determineContextClass获取具体的class,如果配置了contextClass变量,则使用该变量获取class,否则使用默认的初始化,这样applicationContext对象就创建完成了。
然后需要获取配置文件初始化bean。
具体是调用了configureAndRefreshWebApplicationContext方法,打开该方法可以看到:

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {        String configLocationParam;        if(ObjectUtils.identityToString(wac).equals(wac.getId())) {            configLocationParam = sc.getInitParameter("contextId");            if(configLocationParam != null) {                wac.setId(configLocationParam);            } else {                wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath()));            }        }        wac.setServletContext(sc);        configLocationParam = sc.getInitParameter("contextConfigLocation");        if(configLocationParam != null) {            wac.setConfigLocation(configLocationParam);        }        ConfigurableEnvironment env = wac.getEnvironment();        if(env instanceof ConfigurableWebEnvironment) {            ((ConfigurableWebEnvironment)env).initPropertySources(sc, (ServletConfig)null);        }        this.customizeContext(sc, wac);        wac.refresh();    }

可以看到,如果设置了contextConfigLocation初始化参数,则用该参数,否则使用默认参数,这样,application创建bean就齐全了。创建完成后设置进ServletContext中,后续使用只要从里面取出就可以了,key 为ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE。

转载地址:http://fhjqi.baihongyu.com/

你可能感兴趣的文章
深入Hibernate映射文件(二)——<hibernate-mapping>的属性
查看>>
详解在Spring中进行集成测试
查看>>
Struts2中过滤器和拦截器的区别
查看>>
51单片机:led灯闪烁10次后熄灭
查看>>
安卓:okhttp请求,获取返回数据
查看>>
安卓:股票筛选及分析系统
查看>>
增加windows下Tomcat运行时的内存
查看>>
tomcat群集中session共享的几个方案
查看>>
查找google谷歌北京IP地址的方法
查看>>
本科大数据专业该怎么上?
查看>>
云创大数据1+X大数据应用部署与调优职业技能等级证书预申报正式开启!
查看>>
人工智能需要一个可被证明的理论作为基础 | 哈佛丘成桐
查看>>
入门 | 一文概览深度学习中的激活函数
查看>>
一分钟整明白Tensorflow Extended
查看>>
人工智能再次参加高考:和作家比写作文,AI能打多少分?
查看>>
云创冬日紫金山踏雪游记
查看>>
西安思源学院电子信息工程学院院长张卫钢一行到访
查看>>
邀请函|欢迎参加2019云创大数据实验平台金融类/电子商务类/数学统计类院校各省总代理招募大会!...
查看>>
云创大数据的2018年!
查看>>
QNX简介
查看>>