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

What is the process of obtaining a singleton Bean loaded lazily by Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "Spring lazy loading singleton Bean acquisition process is how", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Spring lazy loading singleton Bean acquisition process is how" it!

Preface

The reading of xml should be an important function of Spring, because most of the functions of Spring are based on configuration.

We can read the configuration file in a static code block as follows: / / this loads the configuration file XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource ("beans.xml")); (1) XmlBeanFactory inherits AbstractBeanDefinitionReader and uses ResourceLoader to convert the path of the resource file to the corresponding Resource file. (2) convert Resource file to Document file by DocumentLoader. (3) parse the Document through the DefaultBeanDefinitionDocumentReader class that implements the interface BeanDefinitionDocumentReader, and parse the Element using BeanDefinitionParserDelegate. Step1:

In normal development, we can also use Resource to get the resource file: Resource resource = new ClassPathResource ("application.xml"); InputStream in = resource.getInputStream (); step2:

Before the resource implementation is loaded, super (parentBeanFactory)-/ * * Ignore the given dependency interface for autowiring. (ignore the auto-assembly function of the interface) * / call the loadBeanDefinitions () method of XmlBeanDefinitionReader to load resources: (1) encode Resource resources (2) read XML files through SAX to create InputSource objects (3) Core processing

You can see intuitively that the function is parsing the xml file to get the corresponding Document object.

There is also a method getValidationModeForResource () stored in the doLoadDocument method to read the validation mode of the xml. Conversion to document is also the most commonly used method:! [869effccb2e4f7b69e0b53d17fe0a2b50044d61b] (https://oss-cn-hangzhou.aliyuncs.com/yqfiles/869effccb2e4f7b69e0b53d17fe0a2b50044d61b.png)step3: we have seen step by step how to convert xml files into Document, and now we need to analyze how to extract and register bean. / * * Register the bean definitions contained in the given DOM document*/

The parameter doc is loaded by the loadDocument passed in by the doLoadBeanDefinitions () method. Here is a good embodiment of the object-oriented single full responsibility principle, entrusting logical processing to a single class to deal with.

The single logic processing class here is: BeanDefinitionDocumentReader

Core method: documentReader.registerBeanDefinitions (doc, createReaderContext (resource))

Start parsing:

There are two ways to declare bean in the xml configuration of Spring:

One is default: the other is custom:

< tx : annotation-driven / >

Judged by the default configuration space of the xml configuration file: http://www.springframework.org/schema/beans

For resolution of the default label:

Resolution of Bean configuration:

BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement (ele); returns BeanDefinitionHolder

The code here looks roughly as follows:

Extract id and name attributes from an element

Further parsing encapsulates other attributes into the implementation class of BeanDefinition

If beanName is not specified, the default rule is used to generate beanName.

Wrapper BeanDefinitionHolder

You can first take a look at what the class BeanDefinition does.

BeanDefinition is an interface that corresponds to all the configurations in the configuration file. There are three implementation classes in Spring:

In the configuration file, you can define the parent and the child, where the parent is represented by RootDefinition and the child is represented by ChildBeanDefinition. Spring converts the configuration information in the configuration file into the internal representation of the container through BeanDefiniton, and registers these BeanDefinition with BeanDefinitonRegistry.

The BeanDefinitonRegistry of the Spring container is like an in-memory database of Spring configuration information, mainly in the form of map.

Therefore, the first step in parsing a property is to create an instance to host the property:

Then there are various specific methods for parsing attributes:

Thank you for your reading, the above is the "Spring lazy load singleton Bean acquisition process is how" the content, after the study of this article, I believe you on the Spring lazy load singleton Bean acquisition process is how this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report