上一篇讲解了Spring通过ContextLoader类的initWebApplicationContext
方法创建的WebApplicationContext
实为XmlWebApplicationContext
实例
再看initWebApplicationContext
方法的源码:
/**
* 为了讲重点,省略了部分源码
*/
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
...
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext); //312行
}
if (this.context instanceof ConfigurableWebApplicationContext) {
//314行
//重点就是这里的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 = loadParentC