Let’s learn the SF framework series together-Springframework source code learning summary

learning process

Learning Springframework 6.0.8 took nearly 4 months and finally ended. The main content of learning is shown in the figure (red box):
Insert image description here

This study mainly focuses on the core modules: Beans, Context, Core, SpEL (completely independent of the framework, no in-depth study), AOP, taking the initialization process of SF applications as the axis, and has a deeper understanding of Spring's core technologies dependency injection, events , resources, i18n, validation, data binding, type conversion, SpEL, AOP.

Summary of learning methods

1. Mainly adopt the application tracking method, that is, write the simplest application, and then use the application container ClassPathApplicationContext initialization as the starting point to track the entire initialization process: container initialization, loading resources, loading Bean definitions, and Bean initialization; at the same time, combine the initialization process with various technologies Click to get started and follow up on how to implement the specific technology. Personally, I think the most effective feature of this method is its implementation and result verification, which helps to understand the framework.
2. Just looking at the source code during the learning process is not enough. You also need to do the following:
2.1 Use UML tools to draw class diagrams, so that it is easy to establish an overall impression of relevant points.
2.2 Draw timing diagrams of key processes to help clarify the execution process and jump relationships.
2.3 Take notes, which can be in the form of documents or blogs. In particular, line-by-line comments in the source code are very helpful in understanding the framework.
2.4 Read again and again and revise what you have done in 2.1-2.3.
2.5 Perseverance: Reading the source code will be very uncomfortable at the beginning. The constant jumps seem to be entering a maze. Only persistence can solve the problem.

Springframework understanding summary

1. Springframework has developed to 6.0.8, and the system complexity is very high, as can be seen from the number of classes:
Beans: 313 classes
Context: 517 classes
Core: 634 classes
SpEL: 110 classes
AOP: 203 classes

2. Spring provides dependency generation capabilities, and its framework itself also insists on dependency generation: interface first. Its specific benefits are as follows:
2.1 Interface first, which is the basic mechanism for the framework to provide expansion capabilities. Extensions may not enable applications to intervene or use the frame capabilities while supporting extensions to the frame itself.
2.2 Use of marked interfaces: Marked interfaces refer to interfaces that do not have any methods and only represent certain types of things with a high degree of abstraction. For example, the Aware interface is a typical mark interface, which represents the ability of the application to perceive (acquire) a certain application method (such as ApplicationContextAware, EnvironmentAware, ResourceLoaderAware, MessageSourceAware...) 2.3 Interfaces solve the problem of multiple inheritance: such as the application
container (ApplicationContext) as the application running environment , need to include many different things, which can be unified through the interface, as shown in the figure below:
Insert image description here

3. Deep class inheritance levels: It is common to have more than 5 levels, and it is not uncommon to have more than 9 levels (including interfaces). The advantage of this is that it is very consistent with the OOD principle and is easy to modify and extend; the disadvantage is that the program complexity is greatly increased. Common reading obstacles are: the implementation of a method in the middle of the inheritance hierarchy is an abstract or empty method body, and the specific implementation is in the descendant class. In this case, you need to manually find the implementation method body. An example picture is as follows:
Insert image description here

4. The extension mechanism provided by Spring is also used by the framework itself for extension. For example, there are many extension points in the Bean instantiation and initialization process. Not only can applications use these extension points to take advantage of the framework capabilities, but the framework itself is used in this way. For example, in the parsing BeanDefinition implementation, standard namespace (bean) parsing is the default and non-standard Namespaces (such as context, aop, etc.) are customized entrances. Different element entrances correspond to different parsing implementations. For example, context:annotation-config and context:component-scan correspond to different implementations. The former corresponds to the parser AnnotationConfigBeanDefinitionParser, and the latter corresponds to the parser. ComponentScanBeanDefinitionParser. The advantage of this is of course that it implements the extension mechanism very elegantly. The difficulty is that the readability of the program is greatly reduced. Readers may not even be able to find the correct parser without tracing and debugging. An example picture is as follows:
Insert image description here

Learning gains

By systematically reading the source code of the framework, the following abilities have been improved:
1. The ability to solve application problems: With the deepening of the understanding of the framework, the ability to solve application problems based on the framework has been improved.
2. Improve design capabilities: Learning framework implementation and exploring the design reasons behind it can improve your ability to design and reconstruct application systems.
3. Learn excellent coding style: basically all classes, attributes, and methods have comments, among which interface comments are more detailed. All comments conform to JavaDoc specifications.
4. Learning excellent frameworks can expand readers’ technical horizons and intuitively experience how industry experts solve problems.

At this point, this study is over. Although this study basically understood how the Spring framework is implemented, due to limited personal energy and the complexity of the framework, only about half of the source code was read extensively, and only a quarter was read intensively; for the correctness of the source code understanding, The accuracy cannot be guaranteed to be 100%, which I have to say is a big pity.

Guess you like

Origin blog.csdn.net/davidwkx/article/details/131978738