Spring 配置
在使用 Spring 框架時,最常見的就是需要配置相應的 Spring 配置文件,並在 Web 程序的 web.xml
文件中引入 Spring 。
以下詳細介紹了在 web.xml
文件中配置 Spring 的過程。
Spring 配置文件位置
- 指定位置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
- 默認位置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring 實現自動加載默認配件文件位置
Web 容器啓動後,根據 web.xml
配置,啓動 org.springframework.web.context.ContextLoaderListener
這個用於加載 Spring 應用上下文的監聽器,初始化調用 contextInitialized
方法,其中的 initWebApplicationContext(event.getServletContext())
方法使用到了 ContextLoader
// org.springframework.web.context.ContextLoaderListener
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
由 ContextLoader
中的 static
代碼塊可知,加載了同在此包下的一個 properties 文件 ContextLoader.properties
。
// org.springframework.web.context.ContextLoader
private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";
static {
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());
}
}
根據 ContextLoader.properties
配置,可以反射出一個 XmlWebApplicationContext
上下文。
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
根據 XmlWebApplicationContext
可知默認配置文件位置爲 /WEB-INF/applicationContext.xml
。
// org.springframework.web.context.support.XmlWebApplicationContext
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".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
};
}
}
回頭再看 ContextLoader
中的 configureAndRefreshWebApplicationContext
方法,可知 XmlWebApplicationContext
正是調用了 contextConfigLocation
參數去設置啓動位置。
// org.springframework.web.context.ContextLoader
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
...
wac.setServletContext(sc);
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
wac.setConfigLocation(configLocationParam);
}
...
}
再往上看 XmlWebApplicationContext
繼承的 AbstractRefreshableConfigApplicationContext
類中可以發現對配置文件的 get
和 set
方法。
// org.springframework.context.support.AbstractRefreshableConfigApplicationContext
public void setConfigLocation(String location) {
setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));
}
protected String[] getConfigLocations() {
return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
}