接上一章,我们来具体分析下configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc)方法
//org.springframework.web.context.ContextLoader.class
1 protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { 2 if (ObjectUtils.identityToString(wac).equals(wac.getId())) { 3 // The application context id is still set to its original default value 4 // -> assign a more useful id based on available information 5 String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); 6 if (idParam != null) { 7 wac.setId(idParam); 8 } 9 else { 10 // Generate default id... 11 wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + 12 ObjectUtils.getDisplayString(sc.getContextPath())); 13 } 14 } 15 16 wac.setServletContext(sc); 17 String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); 18 if (configLocationParam != null) { 19 wac.setConfigLocation(configLocationParam); 20 } 21 22 // The wac environment‘s #initPropertySources will be called in any case when the context 23 // is refreshed; do it eagerly here to ensure servlet property sources are in place for 24 // use in any post-processing or initialization that occurs below prior to #refresh 25 ConfigurableEnvironment env = wac.getEnvironment(); 26 if (env instanceof ConfigurableWebEnvironment) { 27 ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); 28 } 29 30 customizeContext(sc, wac); 31 wac.refresh(); 32 }