在spring中,如何实例化了XmlWebApplicationContext
spring的配置文件加载是以监听的方式加载的xml配置文件
ContextLoader类中有一个静态代码块
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());
}
}
DEFAULT_STRATEGIES_PATH="contextLoader.properties";加载的就是这个配置
contextLoader.properties:
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
配置中配置的就是XmlWebApplicationContext
在initWebApplicationContext方法中
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
protected Class<?> determineContextClass(ServletContext servletContext) {
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
if (contextClassName != null) {
try {
return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load custom context class [" + contextClassName + "]", ex);
}
}
else {
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
try {
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load default context class [" + contextClassName + "]", ex);
}
}
}
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());这个代码就是从contextLoad.properties配置中加载进来的。XmlWebApplicationContext
在springmvc中,如何实例化XmlWebApplicationContext的
springmvc加载配置文件
org.springframework.web.servlet.DispatcherServlet是通过这个servlet,加载配置文件
FrameworkServlet中有一个属性
public static final Class<?> DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class;
然而在
protected WebApplicationContext initWebApplicationContext() {
.....
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
}
方法中有一个createWebApplicationContext(rootContext);方法
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
Class<?> contextClass = getContextClass();
if (this.logger.isDebugEnabled()) {
this.logger.debug("Servlet with name '" + getServletName() +
"' will try to create custom WebApplicationContext context of class '" +
contextClass.getName() + "'" + ", using parent context [" + parent + "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '" + getServletName() +
"': custom WebApplicationContext class [" + contextClass.getName() +
"] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
wac.setEnvironment(getEnvironment());
wac.setParent(parent);
wac.setConfigLocation(getContextConfigLocation());
configureAndRefreshWebApplicationContext(wac);
return wac;
}
Class<?> contextClass = getContextClass();这个方法就是创建XmlWebApplicationContext实例的Class
public Class<?> getContextClass() {
return this.contextClass;
}
this.contextClass就是前面FrameworkServlet定义的全局变量