您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何進(jìn)行BeanDefinition的定位,加載和注冊(cè),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
以XmlWebApplicationContext
為例,分析容器初始化過(guò)程中一個(gè)BeanDefinition
的定位,加載和注冊(cè)過(guò)程
Resource
定位。使用ResourceLoader
定位到資源
加載。BeanDefinitionReader
解析資源中的bean的定義信息并創(chuàng)建BeanDefinition
對(duì)象
注冊(cè)。使用BeanDefinitionRegistry
,在容器內(nèi)部將BeanDefinition
對(duì)象注冊(cè)進(jìn)來(lái)
以下是調(diào)用refresh()
方法定位Resource
的棧信息
loadBeanDefinitions:219, AbstractBeanDefinitionReader (org.springframework.beans.factory.support) loadBeanDefinitions:194, AbstractBeanDefinitionReader (org.springframework.beans.factory.support) loadBeanDefinitions:125, XmlWebApplicationContext (org.springframework.web.context.support) loadBeanDefinitions:94, XmlWebApplicationContext (org.springframework.web.context.support) refreshBeanFactory:133, AbstractRefreshableApplicationContext (org.springframework.context.support) obtainFreshBeanFactory:621, AbstractApplicationContext (org.springframework.context.support) refresh:522, AbstractApplicationContext (org.springframework.context.support) ...
調(diào)用BeanDefinitionReader
持有的ResourceLoader
對(duì)象的Resource getResource(String var1);
方法獲取到Resource
ps:在Applicationcontext
的繼承體系中,Applicationcontext
實(shí)現(xiàn)了ResourceLoader
,所以容器也是一個(gè)ResourceLoader
。通常BeanDefinitionReader
持有的ResourceLoader
對(duì)象就是當(dāng)前使用的容器
loadBeanDefinitions:94, XmlWebApplicationContext (org.springframework.web.context.support)
下面的方法對(duì)應(yīng)此棧,方法實(shí)現(xiàn)在XmlWebApplicationContext
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // 創(chuàng)建一個(gè)XmlBeanDefinitionReader,用于定位Bean定義信息 XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); beanDefinitionReader.setEnvironment(getEnvironment()); // XmlWebApplicationContext的繼承體系中實(shí)現(xiàn)了ResourceLoader,所以this是一個(gè)ResourceLoader 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); loadBeanDefinitions(beanDefinitionReader); }
上述方法創(chuàng)建了一個(gè)BeanDefinitionReader
,并將容器作為ResourceLoader
傳遞進(jìn)去
loadBeanDefinitions:219, AbstractBeanDefinitionReader (org.springframework.beans.factory.support)
下面的方法對(duì)應(yīng)此棧,調(diào)用方法的是上一步創(chuàng)建的XmlBeanDefinitionReader
對(duì)象,方法實(shí)現(xiàn)在AbstractBeanDefinitionReader
中
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException { ResourceLoader resourceLoader = getResourceLoader(); if (resourceLoader == null) { throw new BeanDefinitionStoreException("..."); } if (resourceLoader instanceof ResourcePatternResolver) { try { // 至此,完成BeanDefinition的Resource的定位。獲取到Resource Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location); // BenaDefinition的加載和注冊(cè)在此方法中 int loadCount = loadBeanDefinitions(resources); if (actualResources != null) { for (Resource resource : resources) { actualResources.add(resource); } } return loadCount; } catch (IOException ex) { throw new BeanDefinitionStoreException("...", ex); } } else { // 至此,完成BeanDefinition的Resource的定位。獲取到Resource Resource resource = resourceLoader.getResource(location); // BenaDefinition的加載和注冊(cè)均在此方法中 int loadCount = loadBeanDefinitions(resource); if (actualResources != null) { actualResources.add(resource); } if (logger.isDebugEnabled()) { logger.debug("..."); } return loadCount; } }
// 此方法包含BeanDefinition的加載和注冊(cè) processBeanDefinition:305, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) parseDefaultElement:196, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) parseBeanDefinitions:175, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) doRegisterBeanDefinitions:148, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) registerBeanDefinitions:98, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) registerBeanDefinitions:507, XmlBeanDefinitionReader (org.springframework.beans.factory.xml) // 此方法包含了BeanDefinition加載的兩大步驟(解析xml文件創(chuàng)建Document對(duì)象,BeanDefinition加載和注冊(cè)) doLoadBeanDefinitions:391, XmlBeanDefinitionReader (org.springframework.beans.factory.xml) loadBeanDefinitions:335, XmlBeanDefinitionReader (org.springframework.beans.factory.xml) loadBeanDefinitions:303, XmlBeanDefinitionReader (org.springframework.beans.factory.xml) loadBeanDefinitions:187, AbstractBeanDefinitionReader (org.springframework.beans.factory.support) // 這里包含BeanDefinition的定位,同時(shí)也包含BeanDefinition的加載和注冊(cè)的入口方法 loadBeanDefinitions:223, AbstractBeanDefinitionReader (org.springframework.beans.factory.support) ... refresh:522, AbstractApplicationContext (org.springframework.context.support) ...
加載分為兩步
使用XML的解析器獲取到Document
對(duì)象(需要解析xml文件是因?yàn)閎ean的定義信息是在xml文件里寫(xiě)的)
使用DocumentReader
按照Spring
定義Bean
的規(guī)則解析xml文件并創(chuàng)建BeanDefinition
對(duì)象。創(chuàng)建的BeanDefinition
對(duì)象會(huì)先被BeanDefinitionHolder
封裝起來(lái)
doLoadBeanDefinitions:391, XmlBeanDefinitionReader (org.springframework.beans.factory.xml)
下面的方法對(duì)應(yīng)此棧,方法實(shí)現(xiàn)在XmlBeanDefinitionReader
中
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException { try { // 解析xml文件,創(chuàng)建Document對(duì)象 Document doc = doLoadDocument(inputSource, resource); // BeanDefinition的加載和注冊(cè) return registerBeanDefinitions(doc, resource); } catch (Throwable ex) { throw ... } }
parseBeanDefinitions:175, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
下面的方法對(duì)應(yīng)此棧。獲取xml中的根節(jié)點(diǎn),獲取根節(jié)點(diǎn)下的所有子節(jié)點(diǎn),遍歷
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) { 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)) { // 解析根節(jié)點(diǎn)下的子節(jié)點(diǎn) parseDefaultElement(ele, delegate); } else { delegate.parseCustomElement(ele); } } } } else { delegate.parseCustomElement(root); } }
processBeanDefinition:305, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
下面的方法對(duì)應(yīng)此棧。方法的調(diào)用者DefaultBeanDefinitionDocumentReader
是由XmlBeanDefinitionReader
創(chuàng)建的
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) { // 至此,完成BeanDefinition的加載。創(chuàng)建BeanDefinition對(duì)象并將結(jié)果交給BeanDefinitionHolder持有 BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele); if (bdHolder != null) { bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder); try { // BeanDefinition的注冊(cè)在此方法中 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)); } }
registerBeanDefinition:792, DefaultListableBeanFactory (org.springframework.beans.factory.support) registerBeanDefinition:150, BeanDefinitionReaderUtils (org.springframework.beans.factory.support) processBeanDefinition:310, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) parseDefaultElement:196, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) parseBeanDefinitions:175, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) doRegisterBeanDefinitions:148, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) registerBeanDefinitions:98, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml) registerBeanDefinitions:507, XmlBeanDefinitionReader (org.springframework.beans.factory.xml) // 此方法包含了BeanDefinition加載的兩大步驟(解析xml文件創(chuàng)建Document對(duì)象,BeanDefinition加載和注冊(cè)) doLoadBeanDefinitions:391, XmlBeanDefinitionReader (org.springframework.beans.factory.xml) ...
以kv鍵值對(duì)的方式將beanName和BeanDefinition
對(duì)象put到容器內(nèi)部的一個(gè)map中(類型是ConcurrentHashMap
)
processBeanDefinition:310, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)
下面的方法對(duì)應(yīng)此棧
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) { // 創(chuàng)建BeanDefinition并將其包裝在BeanDefinitionHolder中 BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele); if (bdHolder != null) { bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder); try { // 將BeanDefinition注冊(cè)到容器中 // 方法的第二個(gè)參數(shù)是DefaultListableBeanFactory對(duì)象 BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); } catch (BeanDefinitionStoreException ex) { getReaderContext().error("...", ele, ex); } // 發(fā)布注冊(cè)事件 getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder)); } }
registerBeanDefinition:150, BeanDefinitionReaderUtils (org.springframework.beans.factory.support)
下面的方法對(duì)應(yīng)此棧。這里的BeanDefinitionRegistry
的實(shí)際類型是DefaultListableBeanFactory
public static void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) throws BeanDefinitionStoreException { String beanName = definitionHolder.getBeanName(); // 調(diào)用BeanDefinitionRegistry(Bean定義信息注冊(cè)中心)將BeanDefinition以kv形式注冊(cè)到容器中。k是beanName,v是對(duì)象 registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition()); String[] aliases = definitionHolder.getAliases(); if (aliases != null) { for (String alias : aliases) { // 把別名也注冊(cè)到容器中 registry.registerAlias(beanName, alias); } } }
registerBeanDefinition:792, DefaultListableBeanFactory (org.springframework.beans.factory.support)
下面的方法對(duì)應(yīng)此棧。方法實(shí)現(xiàn)在DefaultListableBeanFactory
,方法由BeanDefinitionRegistry
接口定義
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException { if (beanDefinition instanceof AbstractBeanDefinition) { try { ((AbstractBeanDefinition) beanDefinition).validate(); } catch (BeanDefinitionValidationException ex) { throw ... } } BeanDefinition oldBeanDefinition; // 先用beanName從容器中獲取BeanDefinition oldBeanDefinition = this.beanDefinitionMap.get(beanName); // 如果容器中確實(shí)已經(jīng)存在同名BeanDefinition對(duì)象,根據(jù)之前設(shè)置的能否覆蓋同名對(duì)象進(jìn)行操作 if (oldBeanDefinition != null) { // 如果設(shè)置的是 不能覆蓋同名對(duì)象,拋異常 if (!isAllowBeanDefinitionOverriding()) { throw new ... } else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) { ... } else if (!beanDefinition.equals(oldBeanDefinition)) { ... } else { ... } // 如果 能覆蓋同名對(duì)象,將beanDefinition對(duì)象put到map中 // 這個(gè)map就是容器中BeanDefinition的持有者, this.beanDefinitionMap.put(beanName, beanDefinition); } else { if (hasBeanCreationStarted()) { synchronized (this.beanDefinitionMap) { // 將beanDefinition對(duì)象put到map中 this.beanDefinitionMap.put(beanName, beanDefinition); List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1); updatedDefinitions.addAll(this.beanDefinitionNames); updatedDefinitions.add(beanName); this.beanDefinitionNames = updatedDefinitions; if (this.manualSingletonNames.contains(beanName)) { Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames); updatedSingletons.remove(beanName); this.manualSingletonNames = updatedSingletons; } } } else { // 將beanDefinition對(duì)象put到map中 this.beanDefinitionMap.put(beanName, beanDefinition); this.beanDefinitionNames.add(beanName); this.manualSingletonNames.remove(beanName); } this.frozenBeanDefinitionNames = null; } if (oldBeanDefinition != null || containsSingleton(beanName)) { resetBeanDefinition(beanName); } }
此方法會(huì)將BeanDefinition
對(duì)象put到map中
以上就是如何進(jìn)行BeanDefinition的定位,加載和注冊(cè),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。