标签:xmlbeanfactory beandefinitionreader defaultlistablebeanf defaultbeandefinitio
首先,先看我自己画的BeanFactory启动时的时序图。public static void main(String[] args){ BeanFactory factory=new XmlBeanFactory(new ClassPathResource("applicationContext2.xml")); }我们从这一行代码开始。
public class XmlBeanFactory extends DefaultListableBeanFactory { private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); public XmlBeanFactory(Resource resource) throws BeansException { this(resource, null); } public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { super(parentBeanFactory); this.reader.loadBeanDefinitions(resource); } }OK,我们之间开始看XmlBeanDefinitionReader的loadBeanDefinitions。(图中的第二步)
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { .... InputStream inputStream = encodedResource.getResource().getInputStream(); InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) { inputSource.setEncoding(encodedResource.getEncoding()); } return doLoadBeanDefinitions(inputSource, encodedResource.getResource()); .... }上面的EncodedResource是对Resource的再包装,不是重点。重点在doLoadBeanDefinitions
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException { Document doc = this.documentLoader.loadDocument( inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware()); return registerBeanDefinitions(doc, resource); ... //省略try catch }上面的documentLoader是DefaultDocumentLoader的实例,Document是org.w3c.dom.Document的实例。
//XmlBeanDefinitionReader.java public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException { BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader(); documentReader.setEnvironment(this.getEnvironment()); int countBefore = getRegistry().getBeanDefinitionCount(); documentReader.registerBeanDefinitions(doc, createReaderContext(resource)); return getRegistry().getBeanDefinitionCount() - countBefore; }countBefore,就是之前已经注册了多少bean。
//DefaultBeanDefinitionDocumentReader.java public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) { this.readerContext = readerContext; logger.debug("Loading bean definitions"); Element root = doc.getDocumentElement(); doRegisterBeanDefinitions(root); } protected void doRegisterBeanDefinitions(Element root) { this.delegate = createHelper(readerContext, root, parent); //preProcessXml与postProcessXml都是空方法 preProcessXml(root); parseBeanDefinitions(root, this.delegate); postProcessXml(root); this.delegate = parent; } protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) { //<beans xmlns="http://www.springframework.org/schema/beans" /> //查看是否是默认的命名空间 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; if (delegate.isDefaultNamespace(ele)) { //这一行用来出来bean标签 parseDefaultElement(ele, delegate); } else { delegate.parseCustomElement(ele); } } } } else { delegate.parseCustomElement(root); } }我们看看我调试时的一张图
public static final String NAME_ATTRIBUTE = "name"; public static final String BEAN_ELEMENT = "bean"; public static final String META_ELEMENT = "meta"; public static final String ID_ATTRIBUTE = "id"; public static final String PARENT_ATTRIBUTE = "parent"; public static final String CLASS_ATTRIBUTE = "class"; public static final String ABSTRACT_ATTRIBUTE = "abstract"; .....
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) { //是否是import节点 if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) { importBeanDefinitionResource(ele); } else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) { processAliasRegistration(ele); } //是否是bean节点 else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) { processBeanDefinition(ele, delegate); } else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) { // recurse doRegisterBeanDefinitions(ele); } }我们抓主要矛盾,看processBeanDefinition,这已经是图中的第十步了。
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) { BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele); bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder); // Register the final decorated instance. BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder)); }在这里我简单介绍一下BeanDefinitionHolder
public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) { String id = ele.getAttribute(ID_ATTRIBUTE); String nameAttr = ele.getAttribute(NAME_ATTRIBUTE); String beanName = id; AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean); if (beanDefinition != null) { return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray); } return null; }上面的代码,我已经删去了一部分。
//BeanDefinitionParserDelegate.java public AbstractBeanDefinition parseBeanDefinitionElement( Element ele, String beanName, BeanDefinition containingBean) { String className = null; String parent = null; if (ele.hasAttribute(PARENT_ATTRIBUTE)) { parent = ele.getAttribute(PARENT_ATTRIBUTE); } AbstractBeanDefinition bd = createBeanDefinition(className, parent); parseBeanDefinitionAttributes(ele, beanName, containingBean, bd); bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT)); parseMetaElements(ele, bd); parseLookupOverrideSubElements(ele, bd.getMethodOverrides()); parseReplacedMethodSubElements(ele, bd.getMethodOverrides()); parseConstructorArgElements(ele, bd); parsePropertyElements(ele, bd); parseQualifierElements(ele, bd); bd.setResource(this.readerContext.getResource()); bd.setSource(extractSource(ele)); return bd; }parseLookupOverrideSubElements,parseConstructorArgElements,parsePropertyElements大家能猜出来里面是干什么的吗?
//DefaultBeanDefinitionDocumentReader.java protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) { //十一步 BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele); bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder); // Register the final decorated instance. //十二步 BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder)); } //BeanDefinitionReaderUtils.java public static void registerBeanDefinition( BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) throws BeanDefinitionStoreException { // Register bean definition under primary name. String beanName = definitionHolder.getBeanName(); registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition()); }大家得记得,我们的容器,XmlBeanFactory本身也实现了BeanDefinitionRegistry。
//DefaultListableBeanFactory.java public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException { this.beanDefinitionNames.add(beanName); this.beanDefinitionMap.put(beanName, beanDefinition); resetBeanDefinition(beanName); }beanDefinitionMap里面维护着整个容器里面的bean。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Spring揭秘 读书笔记 七 BeanFactory的启动分析
标签:xmlbeanfactory beandefinitionreader defaultlistablebeanf defaultbeandefinitio
原文地址:http://blog.csdn.net/dlf123321/article/details/47984515