In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to build a Spring source operating environment", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to build a Spring source runtime environment".
Why study the source code of Spring
The original intention of studying the source code is to see how powerful the framework is, how elegant the code is, how to use design patterns to build programs, how to achieve beautiful decoupling, how to design extension points flexibly to allow third-party developers to interfere with the operation of the framework, and how to integrate with the framework. Some people may not believe it, but I don't want to study the source code because I want to go to BAT. It is estimated that others will not want it at my age. Laugh at yourself!
I hope to improve the following abilities by reading the Spring source code:
1. First of all, it must be the ability to read the source code, find the basic way to read the source code, improve the ability of DEBUG, and strengthen the skills of how to locate the problem.
two。 Let the elephant in your heart take a small step to familiarize yourself with reading source code, cultivate habits, and see more source code in the future.
3. Find the underlying logic in the source code to write beautiful code, such as design patterns, how to decouple, elegant syntax or API, great utility classes
4. Improve the scaffolding quality of the project, improve the coding ability, and speed up the positioning speed of BUG
5. Share the results of reading the source code, so that passers-by can be interested in reading the source code and solve the problems encountered in the work.
Build Spring source code
The latest Spring source code is built through Gradle, so friends who have only come into contact with Maven construction suggest spending a few hours looking for a Gradle learning video online to learn about grovvy and Gradle, otherwise they really can't understand the gradle.build file. For example, closures, Gradle-specific API, etc., as well as the installation of Gradle, how IDEA improves the speed of Gradle compilation, and so on.
I wrote a blog based on my environment: https://my.oschina.net/u/3049601/blog/3113019 Welcome to refer to
Preparatory work
1. Simply learn grovvy and Gradle related knowledge
two。 Learn about design patterns, such as decorators, template methods, factory methods, and the use of abstract classes and interfaces. A large number of design patterns are used, and the code looks laborious without knowing it in advance.
3. Learn the Lamda expression of Java8
4. Learn the shortcut keys of Idea and some skills of DEBUG
How to look at the Spring source code
After the construction of the Spring source code is completed, how to start to look at, of course, to write a project, through the DEBUG Spring source code to see the execution process, etc., sum up their own experience in looking at the source code
1. Be sure to download the source code from GIT and build it, decompiler to see the source code, especially complex code will be confused.
two。 Open a new project or module in the project, and reference the module in the project for debugging
3. Look at the source code before you must be familiar with the use of the framework, do not be familiar with the use of the framework to go to see the source code, that will certainly be confused. So spend some money to buy some books or videos used in the framework to read first, do not be stingy with a few cents, when you finish reading the salary will certainly rise. For example, SpringMVC, you can't just add @ Controller @ Service @ Component to complete the business code.
4. When looking at the source code, you have a general understanding of the framework through DEBUG. Don't go deep and thin as soon as you come here. In that case, you will find that you won't be able to read it any longer.
When 5.DEBUG, focus on the changes of key variables after the execution of each line of code, such as beanDefinitionMap singletonObjects beanPostProcessors in the Spring source code. Whether the returned object or value may be something you need to pay attention to.
6. Make good use of the conditional breakpoint of DEBUG, otherwise you may not be able to reach the trigger point or execution point you pay attention to even if you press F6 countless times.
7. Be sure to add notes, take notes, draw pictures and so on while watching, because you can't finish it in a day or two. It's likely to be delayed by other things, and you'll be fooled again next time.
8.GOOGLE Baidu is a commonly used tool, you can also try to search gitee or github whether there are source code analysis projects that others have seen, DOWN down and then look, you can improve the reading speed, but you must write code to verify, because someone directly translated English, maybe that does not use Luan. Since it is to look at the source code, is the basic thing, all other people's things must be verified in many ways in order to absorb the correct own, otherwise read for a long time the result is wrong will be troublesome. I encountered such a problem, the content found on the search engine has actually changed in Spring5.
The general workflow of Spring
Start with every potter praises his pot. Because today's development is based on annotations, AnnotationConfigApplicationContext is also used as the analysis object.
Summary process
A very summary diagram depicts the initialization process of the Spring container
Instantiation of Spring core class
The core class acts like the engine of a car. You can't run away without it. Can you count on it to take you to pick up girls and see handsome guys? Similarly, the core class of Spring ensures that the container or context works properly, based on which we manage our business objects and implement business logic, such as:
AnnotationConfigApplicationContext: that is, the whole container of Spring, Spring has another similar one called ClassPathXmlApplicatonContext (based on XML configuration), which contains all instantiated singleton Bean, system parameters and key-value pairs after configuration file loading, post processor, definition of each Bean, and so on.
AnnotatedBeanDefinitionReader: is responsible for reading the annotated Bean definition and adding it to a collection of Bean definitions. A typical scenario is when my ImportResourceAppConfig.class is passed into the AnnotatedBeanDefinitionReader.register () method, and a corresponding element is added to the BeanDefinitionMap.
DefaultListableBeanFactory: this is quite important. It includes registering BeanDefinition-registerBeanDefinition (), and then using BeanDefinition to get or create the logic of Bean-getBean (), corresponding to four variables commonly used by two methods:
Private final Map beanDefinitionMap;-- stores all the Spring Bean definitions (BeanDefinition). For more information about BeanDefinition, please see: https://my.oschina.net/u/3049601/blog/3127635
Private final Map singletonObjects-stores all singleton Spring Bean
Private final Map earlySingletonObjects-Bean that need to be exposed in advance when storing circular dependencies. These Bean are not processed by many PostProcessor (Chinese translation for post processors) to solve the problem of unlimited recursion of circular dependencies (it doesn't matter here, just know that it has a very important relationship with solving circular dependencies, which will be explained later).
Private final Set singletonsCurrentlyInCreation-stores the beanName being created in the current container (it also has a lot to do with circular dependencies)
Registration of core component class definitions
In the figure is a screenshot of the elements of the Bean definition set. These components complete the package scan of the @ ComponentScan annotation, register the scanned class definition, instantiate the bare object, and initialize the SpringBean (including various PostProcessor processing, such as dependency injection).
The classes corresponding to Key in the screenshot Map in the figure are in the following order:
ConfigurationClassPostProcessor: scan @ ComponentScan for all classes that conform to the Spring rules under the specified package path, such as those with @ Configuration @ Component, and register their BeanDefinition with beanDefinitionMaps. Another feature is to add an ImportAwareBeanPostProcessor, which allows your Bean to get the property key-value pairs of custom annotations when using @ Import as meta-annotations (this function can be ignored)
DefaultEventListenerFactory: it has something to do with Application Event. I haven't checked it in detail and will add it later.
EventListenerMethodProcessor: it has something to do with Application Event. I haven't checked it in detail and will add it later.
AutowiredAnnotationBeanPostProcessor: responsible for dependency injection and attribute injection, such as @ Autowired @ Value corresponding actions
CommonAnnotationBeanPostProcessor: responsible for parsing and executing JSR250-related annotations, such as @ PostContruct @ Resource, etc.
Register configuration class
Use AnnotatedBeanDefinitionReader.register (ImportResourceAppConfig.class) to register the BeanDefinition of ImportResourceAppConfig with beanDefinitionMaps. After completing this action, Spring can get the configuration class to do the next step of package scanning, dependency injection and other operations.
Parse the comments on the configuration class and complete the BeanDefinition registration
It is mainly the core functions of ConfigurationClassPostProcessor and AutowiredAnnotationBeanPostProcessor classes (and, of course, other classes involved). For example, ConfigurationClassPostProcessor.ConfigurationClassPaser is responsible for completing package scanning, including the parsing of @ ComponentScan @ Component @ Import @ ImportResource @ Bean annotations to complete the creation of all BeanDefinition and register with beanDefinitionMaps.
Instantiate Spring Bean according to BeanDefinition
After getting all the BeanDefinition ready in the previous step, the next step is to iterate through them, instantiate the object using a dynamic proxy, and end up with a Spring Bean (either a naked object or a proxy object).
Summary: to put it simply, Spring does not create a Bean directly from the Class file, but generates the corresponding BeanDefinition object, and then creates the desired Bean according to it. In this process, Spring provides rich extension points to interfere with the state of BeanDefinition and Bean. These extension points are the reasons why Spring is really powerful.
At this point, I believe you have a deeper understanding of "how to build a Spring source operating environment". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.