Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to locate, load and register BeanDefinition

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares with you is about how to locate, load and register BeanDefinition. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Taking XmlWebApplicationContext as an example, this paper analyzes the positioning, loading and registration of a BeanDefinition during container initialization.

Resource positioning. Navigate to a resource using ResourceLoader

Load. BeanDefinitionReader parses the definition information of bean in the resource and creates a BeanDefinition object

Register. Using BeanDefinitionRegistry, register the BeanDefinition object inside the container

Positioning stack

The following is the stack information that calls the refresh () method to locate the 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). Positioning process

Call the Resource getResource (String var1) of the ResourceLoader object held by BeanDefinitionReader; the method gets the Resource

Ps: in Applicationcontext's inheritance architecture, Applicationcontext implements ResourceLoader, so the container is also a ResourceLoader. Usually, the ResourceLoader object held by BeanDefinitionReader is the container currently used.

LoadBeanDefinitions:94, XmlWebApplicationContext (org.springframework.web.context.support)

The following method corresponds to this stack, which is implemented in XmlWebApplicationContext

Protected void loadBeanDefinitions (DefaultListableBeanFactory beanFactory) throws BeansException, IOException {/ / create a XmlBeanDefinitionReader to locate the Bean definition information XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader (beanFactory); beanDefinitionReader.setEnvironment (getEnvironment ()); / / XmlWebApplicationContext implements ResourceLoader in the inheritance system, so this is a 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);}

The above method creates a BeanDefinitionReader and passes the container as a ResourceLoader

LoadBeanDefinitions:219, AbstractBeanDefinitionReader (org.springframework.beans.factory.support)

The following method corresponds to this stack. The method is called by the XmlBeanDefinitionReader object created in the previous step, and the method is implemented in AbstractBeanDefinitionReader.

Public int loadBeanDefinitions (String location, @ Nullable Set actualResources) throws BeanDefinitionStoreException {ResourceLoader resourceLoader = getResourceLoader (); if (resourceLoader = = null) {throw new BeanDefinitionStoreException ("...");} if (resourceLoader instanceof ResourcePatternResolver) {try {/ / so far, complete the positioning of BeanDefinition's Resource. Get the loading and registration of Resource Resource [] resources = ((ResourcePatternResolver) resourceLoader) .getResources (location); / / BenaDefinition in this method int loadCount = loadBeanDefinitions (resources); if (actualResources! = null) {for (Resource resource: resources) {actualResources.add (resource) }} return loadCount;} catch (IOException ex) {throw new BeanDefinitionStoreException ("...", ex);}} else {/ / so far, complete the positioning of the Resource of BeanDefinition. Get Resource Resource resource = resourceLoader.getResource (location); / / load and register BenaDefinition in this method int loadCount = loadBeanDefinitions (resource); if (actualResources! = null) {actualResources.add (resource);} if (logger.isDebugEnabled ()) {logger.debug ("...");} return loadCount }} loaded stack / / this method includes processBeanDefinition:305 for loading and registering BeanDefinition, 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) / / this method includes two steps of BeanDefinition loading (parsing the xml file to create the Document object BeanDefinition loading and registration) 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) / / contains the location of BeanDefinition It also contains BeanDefinition loading and registration entry methods loadBeanDefinitions:223, AbstractBeanDefinitionReader (org.springframework.beans.factory.support)... refresh:522, AbstractApplicationContext (org.springframework.context.support). Loading process

Loading is divided into two steps

Use XML's parser to get the Document object (you need to parse the xml file because the definition information of bean is written in the xml file)

Use DocumentReader to parse the xml file and create a BeanDefinition object according to the rules that Spring defines Bean. The created BeanDefinition object will be encapsulated by BeanDefinitionHolder first

DoLoadBeanDefinitions:391, XmlBeanDefinitionReader (org.springframework.beans.factory.xml)

The following method corresponds to this stack, and the method is implemented in XmlBeanDefinitionReader

Protected int doLoadBeanDefinitions (InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {try {/ / parse the xml file and create the Document object Document doc = doLoadDocument (inputSource, resource); / / load and register return registerBeanDefinitions (doc, resource) for BeanDefinition;} catch (Throwable ex) {throw.} parseBeanDefinitions:175, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)

The following methods correspond to this stack. Get the root node in xml, get all the child nodes under the root node, and traverse

Protected void parseBeanDefinitions (Element root, BeanDefinitionParserDelegate delegate) {if (delegate.isDefaultNamespace (root)) {NodeList nl = root.getChildNodes (); for (int I = 0; I < nl.getLength (); iTunes +) {Node node = nl.item (I) If (node instanceof Element) {Element ele = (Element) node If (delegate.isDefaultNamespace (ele)) {/ / parse the child node parseDefaultElement (ele, delegate) under the root node } else {delegate.parseCustomElement (ele) } else {delegate.parseCustomElement (root);}} processBeanDefinition:305, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)

The following methods correspond to this stack. The caller DefaultBeanDefinitionDocumentReader of the method is created by XmlBeanDefinitionReader

Protected void processBeanDefinition (Element ele, BeanDefinitionParserDelegate delegate) {/ / at this point, finish loading the BeanDefinition. Create a BeanDefinition object and give the result to BeanDefinitionHolder to hold BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement (ele); if (bdHolder! = null) {bdHolder = delegate.decorateBeanDefinitionIfRequired (ele, bdHolder); registration of try {/ / BeanDefinition in this method 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)) }} registered stack 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) / / this method includes two steps of BeanDefinition loading (parsing the xml file to create the Document object BeanDefinition load and register) doLoadBeanDefinitions:391, XmlBeanDefinitionReader (org.springframework.beans.factory.xml)... Registration proc

Put the beanName and BeanDefinition objects into a map inside the container as kv key-value pairs (type is ConcurrentHashMap)

ProcessBeanDefinition:310, DefaultBeanDefinitionDocumentReader (org.springframework.beans.factory.xml)

The following methods correspond to this stack

Protected void processBeanDefinition (Element ele, BeanDefinitionParserDelegate delegate) {/ / create BeanDefinition and wrap it in BeanDefinitionHolder BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement (ele); if (bdHolder! = null) {bdHolder = delegate.decorateBeanDefinitionIfRequired (ele, bdHolder); try {/ / register BeanDefinition into the container / / the second parameter of the method is the DefaultListableBeanFactory object BeanDefinitionReaderUtils.registerBeanDefinition (bdHolder, getReaderContext (). GetRegistry ()) } catch (BeanDefinitionStoreException ex) {getReaderContext () .error ("...", ele, ex);} / / publish registration event getReaderContext () .fireComponentRegistered (new BeanComponentDefinition (bdHolder));}} registerBeanDefinition:150, BeanDefinitionReaderUtils (org.springframework.beans.factory.support)

The following methods correspond to this stack. The actual type of BeanDefinitionRegistry here is DefaultListableBeanFactory

Public static void registerBeanDefinition (BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) throws BeanDefinitionStoreException {String beanName = definitionHolder.getBeanName (); / / call BeanDefinitionRegistry (Bean definition information registry) to register BeanDefinition in the container as kv. K is beanName,v is the object registry.registerBeanDefinition (beanName, definitionHolder.getBeanDefinition ()); String [] aliases = definitionHolder.getAliases (); if (aliases! = null) {for (String alias: aliases) {/ / register aliases to registry.registerAlias (beanName, alias);} registerBeanDefinition:792, DefaultListableBeanFactory (org.springframework.beans.factory.support)

The following methods correspond to this stack. Method is implemented in DefaultListableBeanFactory, and method is defined by BeanDefinitionRegistry interface

Public void registerBeanDefinition (String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException {if (beanDefinition instanceof AbstractBeanDefinition) {try {((AbstractBeanDefinition) beanDefinition). Validate ();} catch (BeanDefinitionValidationException ex) {throw...} BeanDefinition oldBeanDefinition; / / first use beanName to get BeanDefinition oldBeanDefinition = this.beanDefinitionMap.get (beanName) from the container / / if a BeanDefinition object with the same name does exist in the container, operate if (oldBeanDefinition! = null) {/ / if the object with the same name cannot be overwritten Throw exception if (! isAllowBeanDefinitionOverriding ()) {throw new.} else if (oldBeanDefinition.getRole () < beanDefinition.getRole ()) {...} else if (! beanDefinition.equals (oldBeanDefinition)) {.} else {.} / / if you can overwrite an object with the same name Put the beanDefinition object into the map / / this map is the holder of the BeanDefinition in the container, this.beanDefinitionMap.put (beanName, beanDefinition) } else {if (hasBeanCreationStarted ()) {synchronized (this.beanDefinitionMap) {/ / put beanDefinition objects into map this.beanDefinitionMap.put (beanName, beanDefinition); List updatedDefinitions = new ArrayList (this.beanDefinitionNames.size () + 1); updatedDefinitions.addAll (this.beanDefinitionNames); updatedDefinitions.add (beanName) This.beanDefinitionNames = updatedDefinitions; if (this.manualSingletonNames.contains (beanName)) {Set updatedSingletons = new LinkedHashSet (this.manualSingletonNames); updatedSingletons.remove (beanName); this.manualSingletonNames = updatedSingletons } else {/ / put beanDefinition objects into map this.beanDefinitionMap.put (beanName, beanDefinition); this.beanDefinitionNames.add (beanName); this.manualSingletonNames.remove (beanName);} this.frozenBeanDefinitionNames = null } if (oldBeanDefinition! = null | | containsSingleton (beanName)) {resetBeanDefinition (beanName);}}

This method put the BeanDefinition object into map

The above is how to locate, load and register BeanDefinition. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report