技术开发 频道

初探spring applicationContext


IT168技术文档】 
    找到同级目录下的配置文件 
    ==>ContextLoader.properties
# Default WebApplicationContext implementation class for ContextLoader. # Used as fallback when no explicit context implementation has been specified as
context-param. # Not meant to be customized by application developers. <span style="color:red;"> #默认的WebApplicationContext为org.springframework.web.context.support.
XmlWebApplicationContext org.springframework.web.context.WebApplicationContext
=org.springframework.web.
context.support.XmlWebApplicationContext
</span>
==>org.springframework.web.context.support.XmlWebApplicationContext
public class XmlWebApplicationContext extends
AbstractRefreshableWebApplicationContext
{ /** Default config location for the root context */ <span style="color:red;"> //配置了默认的spring配置文件 public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"; </span> //配置文件默认BUILD路径 public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/"; //配置文件默认后缀名 public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml"; /** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ //获得bean配置 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws
IOException
{ //从BEAN工厂获得一个XmlBeanDefinitionReader 来读取SPRING配置文件 XmlBeanDefinitionReader beanDefinitionReader = new
XmlBeanDefinitionReader(beanFactory);
//设置beanDefinitionReader服务于当前CONTEXT // resource loading environment. beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader); <span style="color:red;"> //读取配置文件 loadBeanDefinitions(beanDefinitionReader); </span> }
/** * Initialize the bean definition reader used for loading the bean * definitions of this context. Default implementation is empty. * <p>Can be overridden in subclasses, e.g. for turning off XML validation * or using a different XmlBeanDefinitionParser implementation. * @param beanDefinitionReader the bean definition reader used by this context * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setValidationMode * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
#setDocumentReaderClass
*/
protected void initBeanDefinitionReader(XmlBeanDefinitionReader
beanDefinitionReader)
{ } /** * Load the bean definitions with the given XmlBeanDefinitionReader. * <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method; * therefore this method is just supposed to load and/or register bean definitions. * <p>Delegates to a ResourcePatternResolver for resolving location patterns * into Resource instances. * @throws org.springframework.beans.BeansException in case of bean registration errors * @throws java.io.IOException if the required XML document isn't found * @see #refreshBeanFactory * @see #getConfigLocations * @see #getResources * @see #getResourcePatternResolver */ <span style="color:red;"> //读取配置文件 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws
BeansException, IOException
{ String[] configLocations = getConfigLocations(); if (configLocations != null) { for (int i = 0; i < configLocations.length; i++) { reader.loadBeanDefinitions(configLocations[i]); } } } </span> /** * The default location for the root context is "/WEB-INF/applicationContext.xml", * and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet" * (like for a DispatcherServlet instance with the servlet-name "test"). */ //获得默认的ConfigLocations 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}; } }


0
相关文章