相信大家spring的操作都能够熟练的运用了,对于spring的一些内部实现原理是怎么样的呢?spring的IOC是怎么整合到web容器中的呢。
在启动时spring会加载ContextLoader.properties文件:org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
从中可以看出XmlWebApplicationContext是web应用加载的类,spring的继承还是比较复杂的,下面用简单的图表示下
/** Default config location for the root context */
默认的加载的配置文件
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
/** Default prefix for building a config location for a namespace */
public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
/** 默认文件的后缀名*/
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
//这个方法是加载配置文件,如果没有设置namespace则加载/WEB-INF/applicationContext.xml,想spring mvc则会设置namespace,所以
//配置的时候需要用/WEB-INF/xxx.xml,这个以后再讲
protected String[] getDefaultConfigLocations() {
if (getNamespace() != null) {
return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
}
else {
return new String[] {DEFAULT_CONFIG_LOCATION};
}
}
//这边是加载bean配置的地方,稍后详解
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
//第一个被加载的配置文件
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}
}
}
三到四