二Spring组装BeanDefinition详情

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二Spring组装BeanDefinition详情相关的知识,希望对你有一定的参考价值。

以下是Spring加载Xml,封装BeanDefiniton的概要代码

主要有这些类

*XmlBeanDefinitionReader

*DefaultBeanDefinitionDocumentReader

*BeanDefinitionParserDelegate



1)[AbstractApplicationContext] ConfigurableListableBeanFactory obtainFreshBeanFactory()

refreshBeanFactory();


2) [AbstractRefreshableApplicationContext] refreshBeanFactory()

DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory()

loadBeanDefinitions(beanFactory);


3)[AbstractXmlApplicationContext] loadBeanDefinitions(DefaultListableBeanFactory beanFactory)

XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

loadBeanDefinitions(beanDefinitionReader);


4)[AbstractXmlApplicationContext] loadBeanDefinitions(XmlBeanDefinitionReader reader)

String[] configLocations = getConfigLocations();

reader.loadBeanDefinitions(configLocations);


5)[AbstractBeanDefinitionReader] loadBeanDefinitions(String... locations)

for (String location : locations) {counter += loadBeanDefinitions(location);}


6)[XmlBeanDefinitionReader] doLoadBeanDefinitions(InputSource inputSource, Resource resource)

Document doc = this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());

return registerBeanDefinitions(doc, resource); ->

 [DefaultDocumentLoader] Document loadDocument(InputSource inputSource, EntityResolver entityResolver,ErrorHandler errorHandler, int validationMode, boolean namespaceAware)

javax.xml.parsers.DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = factory.newDocumentBuilder(); docBuilder.setEntityResolver(entityResolver); return docBuilder.parse(inputSource);


7)[XmlBeanDefinitionReader] registerBeanDefinitions(doc, resource); 

BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();[DefaultBeanDefinitionDocumentReader]

documentReader.registerBeanDefinitions(doc, createReaderContext(resource));        

  

8)[XmlBeanDefinitionReader]  XmlReaderContext createReaderContext(Resource resource)

namespaceHandlerResolver=new DefaultNamespaceHandlerResolver(getResourceLoader().getClassLoader());

 return new XmlReaderContext(resource, this.problemReporter, this.eventListener,this.sourceExtractor, this, this.namespaceHandlerResolver);


9)[DefaultBeanDefinitionDocumentReader] registerBeanDefinitions(doc,readerContext)

Element root = doc.getDocumentElement(); 

doRegisterBeanDefinitions(root);


10)[DefaultBeanDefinitionDocumentReader] doRegisterBeanDefinitions(root)

BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, environment); parseBeanDefinitions(root, this.delegate);


11)[DefaultBeanDefinitionDocumentReader] parseBeanDefinitions(rElement root, BeanDefinitionParserDelegate delegate)

NodeList nl = root.getChildNodes();

for (int i = 0; i < nl.getLength(); i++) {Node node = nl.item(i);Element ele = (Element) node;

if (delegate.isDefaultNamespace(ele)) {parseDefaultElement(ele, delegate);}

else {delegate.parseCustomElement(ele);}  }

[return "http://www.springframework.org/schema/beans".equals(node.getNamespaceURI());]


12)[DefaultBeanDefinitionDocumentReader]parseDefaultElement(ele, delegate)

if (delegate.nodeNameEquals(ele, "import")) {importBeanDefinitionResource(ele);}

else if (delegate.nodeNameEquals(ele, "bean")) {processBeanDefinition(ele, delegate);}





以上是关于二Spring组装BeanDefinition详情的主要内容,如果未能解决你的问题,请参考以下文章

spring核心之BeanDefinition

spring核心之BeanDefinition

通过BeanDefinitionReader获取BeanDefinition的三种不同方式

Spring IocDI

spring源码阅读-- 容器启动之加载BeanDefinition

Spring自定义标签