// 加载 xml 配置文件, 并注册 beanDefinition @Override protectedvoidloadBeanDefinitions(DefaultListableBeanFactory beanFactory)throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. // 创建一个 XmlBeanDefinitionReader, 并通过回调设置到 beanFactory 中 XmlBeanDefinitionReaderbeanDefinitionReader=newXmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's // resource loading environment. // 给 beanDefinitionReader 对象设置对象 beanDefinitionReader.setEnvironment(this.getEnvironment()); beanDefinitionReader.setResourceLoader(this); // 设置要用于解析的 SAX 实体解析器 beanDefinitionReader.setEntityResolver(newResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. // 初始化 beanDefinitionReader 对象,此处设置配置文件是否要进行验证 initBeanDefinitionReader(beanDefinitionReader); // 加载并注册 beanDefinition loadBeanDefinitions(beanDefinitionReader); }
publicintloadBeanDefinitions(EncodedResource encodedResource)throws BeanDefinitionStoreException { Assert.notNull(encodedResource, "EncodedResource must not be null"); if (logger.isTraceEnabled()) { logger.trace("Loading XML bean definitions from " + encodedResource); }
if (this.delegate.isDefaultNamespace(root)) { StringprofileSpec= root.getAttribute(PROFILE_ATTRIBUTE); if (StringUtils.hasText(profileSpec)) { String[] specifiedProfiles = StringUtils.tokenizeToStringArray( profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS); // We cannot use Profiles.of(...) since profile expressions are not supported // in XML config. See SPR-12458 for details. if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) { if (logger.isDebugEnabled()) { logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec + "] not matching: " + getReaderContext().getResource()); } return; } } } // 解析 xml 的前置逻辑(扩展点, 钩子方法) preProcessXml(root); // 真正的解析方法, 处理默认命名空间的标签("import"、"alias"、"bean") 和 自定义的标签 parseBeanDefinitions(root, this.delegate); // 解析 xml 的后置处理逻辑(扩展点, 钩子方法) postProcessXml(root);