1、具体会构建哪一种WebApplicationContext?
容器会首先去获取web.xml文件中contextClass参数的配置,如果其没有进行配置,则采用默认的策略,构建XmlWebApplicationContext。
在ContextLoader类中初始化的时候,有一段静态代码块,其中初始化了Resource,读取
了ContextLoader.properties文件。
static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
}
2、构建WebApplicationContext的核心源码
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
//构建一个Class:
org.springframework.web.context.support.XmlWebApplicationContext
Class<?> contextClass = determineContextClass(sc);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
//返回一个实例化的对象
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}