Springソースレコードを読む


1.Springのコンテナ作成リスナーContextLoaderクラスにおけるinitWebapplicationメソッド
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!");
		}
    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);
	    }
            //         
	    configureAndRefreshWebApplicationContext(cwac, servletContext);
	}
    }
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
}

2.configureAndRefreshWebAplicationContext(cwac,servletContext)のうちの1つ
//           
	protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
		wac.setServletContext(sc);
		//   Spring       
		String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
		if (configLocationParam != null) {
			wac.setConfigLocation(configLocationParam);
		}
		ConfigurableEnvironment env = wac.getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
		}
		//     (   )
		customizeContext(sc, wac);
		wac.refresh();
	}

3、customizeContext(sc,wac)カスタムコンテナ(拡張コンテナの初期化を提供する)ApplicationContextInitializer
ApplicationContextInitalizerにはinitialize(C o f i g u r a b eApplicationContextアプリケーションContext)というメソッドが1つしかありません.
protected List>>
		determineContextInitializerClasses(ServletContext servletContext) {
		List>> classes =
				new ArrayList>>();
		//           
		String globalClassNames = servletContext.getInitParameter(GLOBAL_INITIALIZER_CLASSES_PARAM);
		if (globalClassNames != null) {
			for (String className : StringUtils.tokenizeToStringArray(globalClassNames, INIT_PARAM_DELIMITERS)) {
				classes.add(loadInitializerClass(className));
			}
		}
		//           
		String localClassNames = servletContext.getInitParameter(CONTEXT_INITIALIZER_CLASSES_PARAM);
		if (localClassNames != null) {
			for (String className : StringUtils.tokenizeToStringArray(localClassNames, INIT_PARAM_DELIMITERS)) {
				classes.add(loadInitializerClass(className));
			}
		}
		return classes;
	}
	
	protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) {
		//   ServletContext                   
		List>> initializerClasses =
				determineContextInitializerClasses(sc);

		for (Class> initializerClass : initializerClasses) {
			Class> initializerContextClass =
					GenericTypeResolver.resolveTypeArgument(initializerClass, ApplicationContextInitializer.class);
			if (initializerContextClass != null && !initializerContextClass.isInstance(wac)) {
				throw new ApplicationContextException(String.format(
						"Could not apply context initializer [%s] since its generic parameter [%s] " +
						"is not assignable from the type of application context used by this " +
						"context loader: [%s]", initializerClass.getName(), initializerContextClass.getName(),
						wac.getClass().getName()));
			}
			//           map 
			this.contextInitializers.add(BeanUtils.instantiateClass(initializerClass));
		}
		AnnotationAwareOrderComparator.sort(this.contextInitializers);
		for (ApplicationContextInitializer initializer : this.contextInitializers) {
			//       
			initializer.initialize(wac);
		}
	}