在Spring中,系统已经为用户提供了许多已经定义好的容器实现,而不须要开发人员事必躬亲。相比那些简单拓展BeanFactory的基本IoC容器,开发人员经常使用的ApplicationContext除T可以提供前面介绍的容器的基本功能外,还为用户提供了附加服务,可让客户更方便地使用。因此说,ApplicationContext是一个高级形态意义的IoC容器,如上图所示,能够看到ApplicationContext在BeanFactory的基础上添加的附加功能,这些功能为ApplicationContext提供了如下BeanFactory不具有的新特性。java
在ApplicationContext容器中,以经常使用的FileSystemXmlApplicationContext的实现为例来讲明ApplcationContext的设计原理。 在FileSystemXmlApplicationContext的设计中,ApplicationContext应用上下文的主要功能已经在FileSystemXmlApplicationContext前面的基类们中完成,主要功能是在AbstractXmlApplicationContext中实现的。在FileSystemXmlApplicationContext中,做为一个具体的应用上下文,只须要实现和它自身设计相关的两个功能。web
/** * Create a new FileSystemXmlApplicationContext with the given parent, * loading the definitions from the given XML files. * @param configLocations array of file paths * @param refresh whether to automatically refresh the context, * loading all bean definitions and creating all singletons. * Alternatively, call refresh manually after further configuring the context. * @param parent the parent context * @throws BeansException if context creation failed * @see #refresh() */ public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }
这个refresh()过程会牵涉IoC容器启动的一系列复杂操做,至关于一个入口,同时,对于不一样的容器实现,这些操做都是相似的,所以在基类中将它们封装好(因此主要功能都是在基类中完成的)。因此,咱们在FileSystemXml的设计中看到的只是一个简单的调用。spring
/** * Resolve resource paths as file system paths. * <p>Note: Even if a given path starts with a slash, it will get * interpreted as relative to the current VM working directory. * This is consistent with the semantics in a Servlet container. * @param path path to the resource * @return Resource handle * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath */ @Override protected Resource getResourceByPath(String path) { if (path != null && path.startsWith("/")) { path = path.substring(1); } return new FileSystemResource(path); }