ClassPathXmlApplicationContext
虽然实现了BeanFactory接口,但是只是自己持有一个BeanFactory的实现类
DefaultListableBeanFactory
BeanFactory是Bean工厂,可以从中获取交给Spring管理的Bean对象
Registry是用来注册和存储管理BeanDefinition(由BeanDefinitionRegistry定义)和bean对象(由BeanRegistry定义)的
XmlBeanDefinitionReader
到XmlBeanDefinitionReader的loadBeanDefinitions(EncodedResource encodedResource)开始解析xml文件
EncodedResource是带编码格式的Resource,Resource里面是一个类路径和一个类加载器
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { Assert.notNull(encodedResource, "EncodedResource must not be null"); if (logger.isTraceEnabled()) { logger.trace("Loading XML bean definitions from " + encodedResource); } //拿当前线程已经加载过的所有encodedResource Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get(); //encodedResource加入threadLocal-set,如失败则说明当前encodedResource已经加载过了,不能重复加载 if (!currentResources.add(encodedResource)) { throw new BeanDefinitionStoreException( "Detected cyclic loading of " + encodedResource + " - check your import definitions!"); } //处理encodedResource try (InputStream inputStream = encodedResource.getResource().getInputStream()) { InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) { inputSource.setEncoding(encodedResource.getEncoding()); } //加载BeanDefinition return doLoadBeanDefinitions(inputSource, encodedResource.getResource()); } catch (IOException ex) { throw new BeanDefinitionStoreException( "IOException parsing XML document from " + encodedResource.getResource(), ex); } finally { //当前这个resource处理完,需要从set中移除 currentResources.remove(encodedResource); //防止threadLocal内存泄露。 if (currentResources.isEmpty()) { this.resourcesCurrentlyBeingLoaded.remove(); } } }
其中doLoadBeanDefinitions(inputSource, encodedResource.getResource())主要是将resource转换为document对象,
然后解析成BeanDefinitions,再将BeanDefinitions注册到BeanFactory中,最后返回新注册BeanDefinition数量
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException { try { //将resource转换成程序层面可以识别的有层次结构的 document 对象。 Document doc = doLoadDocument(inputSource, resource); //将document解析成bd并且注册到bf中,最终返回新注册到bf中的bd数量。 int count = registerBeanDefinitions(doc, resource); if (logger.isDebugEnabled()) { logger.debug("Loaded " + count + " bean definitions from " + resource); } //返回新注册bd的数量。 return count; } catch (BeanDefinitionStoreException ex) { throw ex; } catch (SAXParseException ex) { throw new XmlBeanDefinitionStoreException(resource.getDescription(), "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex); } catch (SAXException ex) { throw new XmlBeanDefinitionStoreException(resource.getDescription(), "XML document from " + resource + " is invalid", ex); } catch (ParserConfigurationException ex) { throw new BeanDefinitionStoreException(resource.getDescription(), "Parser configuration exception parsing XML from " + resource, ex); } catch (IOException ex) { throw new BeanDefinitionStoreException(resource.getDescription(), "IOException parsing XML document from " + resource, ex); } catch (Throwable ex) { throw new BeanDefinitionStoreException(resource.getDescription(), "Unexpected exception parsing XML document from " + resource, ex); } }
registerBeanDefinitions应用了面向对象中单一职责的原则,将逻辑处理委托给单一的类进行处理,而这个逻辑处理类就是BeanDefinitionDocumentReader;
BeanDefinitionDocumentReader是一个接口,通过createBeanDefinitionDocumentReader()拿到的是是DefaultBeanDefinitionDocumentReader了;
其中documentReader.registerBeanDefinitions(doc, createReaderContext(resource))这个方法的重要目的之一就是提取root,以便于再次将root作为参数继续BeanDefinition的注册。
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException { BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader(); //getRegistry()返回创建的BeanFactory实例,就是DefaultListableBeanFactory int countBefore = getRegistry().getBeanDefinitionCount(); //解析doc 并且注册到BeanFactory中。 documentReader.registerBeanDefinitions(doc, createReaderContext(resource)); //返回的新注册的bd数量。 return getRegistry().getBeanDefinitionCount() - countBefore; }
通过doc.getDocumentElement()拿到Element root,表示的是document代表的xml的顶层标签 =>
@Override public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) { this.readerContext = readerContext; doRegisterBeanDefinitions(doc.getDocumentElement()); }
doRegisterBeanDefinitions(Element root)方法是核心部分;
createDelegate(getReaderContext(), root, parent)给出bean标签解析器对象BeanDefinitionParserDelegate,用于解析beanName、别名以及各种默认的属性;
parseBeanDefinitions(root, this.delegate)解析"import", "alias", "bean"这些子标签;
preProcessXml(root)和postProcessXml(root)都是用于子类扩展。
protected void doRegisterBeanDefinitions(Element root) { BeanDefinitionParserDelegate parent = this.delegate; //返回一个beans标签解析器对象。 this.delegate = createDelegate(getReaderContext(), root, parent); if (this.delegate.isDefaultNamespace(root)) { //获取beans标签 属性 profile String profileSpec = 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; } } } //子类扩展 preProcessXml(root); parseBeanDefinitions(root, this.delegate); //子类扩展 postProcessXml(root); this.delegate = parent; }
parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)
迭代处理每个子标签,分为默认和自定义两种,分别处理。
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) { //条件成立:说明root是spring命名空间。 if (delegate.isDefaultNamespace(root)) { NodeList nl = root.getChildNodes(); //迭代处理每个子标签 for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element ele = (Element) node; //子标签是spring默认标签。 if (delegate.isDefaultNamespace(ele)) { //解析默认标签 parseDefaultElement(ele, delegate); } else { //解析自定义标签 delegate.parseCustomElement(ele); } } } } else { delegate.parseCustomElement(root); } }
parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate)
根据Element标签名匹配不同处理方法;
其中嵌套就是递归调用doRegisterBeanDefinitions(Element ele)。
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) { //import标签 if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) { importBeanDefinitionResource(ele); } //alias标签 else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) { processAliasRegistration(ele); } //bean标签 else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) { processBeanDefinition(ele, delegate); } //嵌套beans标签 else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) { // recurse doRegisterBeanDefinitions(ele); } }
processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate)
处理BeanDefinition包括解析以及注册
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) { //bdHolder保存bean标签上的别名信息 BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele); if (bdHolder != null) { //装饰beanDefinition,主要是处理自定义属性。 bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder); try { // Register the final decorated instance. //将bd注册到容器中。 BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); } catch (BeanDefinitionStoreException ex) { getReaderContext().error("Failed to register bean definition with name '" + bdHolder.getBeanName() + "'", ele, ex); } // Send registration event. getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder)); } }
https://blog.csdn.net/YangLiehui/article/details/98847317?spm=1001.2014.3001.5501
https://book.douban.com/subject/25866350/
https://blog.csdn.net/wang704987562/article/details/81273899