// Load Spring web application configuration AnnotationConfigWebApplicationContextac=newAnnotationConfigWebApplicationContext(); ac.register(AppConfig.class); ac.refresh();
// Create and register the DispatcherServlet DispatcherServletservlet=newDispatcherServlet(ac); ServletRegistration.Dynamicregistration= servletCxt.addServlet("app", servlet); registration.setLoadOnStartup(1); registration.addMapping("/app/*"); } }
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { // 检查有没有绑定上下文 if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { thrownewIllegalStateException( "Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader* definitions in your web.xml!"); }
servletContext.log("Initializing Spring root WebApplicationContext"); Loglogger= LogFactory.getLog(ContextLoader.class); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext: initialization started"); } // 初始化开始时间 longstartTime= System.currentTimeMillis();
try { // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. // 保存上下文到本地实例变量中,保证上细纹能在 ServletContext 关闭时访问到 if (this.context == null) { // 创建上下文 this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { // 如果上下文实现了 ConfigurableWebApplicationContext ConfigurableWebApplicationContextcwac= (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. ApplicationContextparent= loadParentContext(servletContext); cwac.setParent(parent); } // 设置和刷新上下文 configureAndRefreshWebApplicationContext(cwac, servletContext); } } // 将上下文绑定到 servletContext 的属性上 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
protectedvoidconfigureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { // 用可以获取到的信息,获取一个更有意义的上下文 if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information // 获取 ServletContext 的 'contextId' 初始化参数。 StringidParam= sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... // 生成默认 id wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } // 设置 servletContext 属性 wac.setServletContext(sc); // 设置配置文件路径 StringconfigLocationParam= sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); }
// The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh // 初始化属性源, 确保 servlet 属性源到位并能够在任何 refresh 之前的后期处理和初始化中使用 ConfigurableEnvironmentenv= wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); }