This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Today, I will sort out the overall context of Spring, paving the way for the following article
These should be covered in the next few articles 😆
- Spring AOP plugin 🐖 (🕊)
- Share the story of 4ye refactoring the project using AOP + MybatisPlus to optimize the special logging module in the system
- Spring property injection, loop dependencies, transactions, etc
- Spring in the source points AbstractApplicationContext the refresh method in 12 method
- Design patterns in Spring and so on
- At present think so much 🐷
We have been using Spring all the time. What are your feelings about it? Still can’t say lol
The feeling of 4YE is also scattered, and the impression has been using Springboot, do not have to configure a bunch of things ah, management depends on what, too convenient.
So take this opportunity to simply comb some of the context, so to see the source code is more organized, more can know the use of some extension points such as 😝
directory
This article will briefly introduce these knowledge points 👇
Spring in my mind
I have this formula in mind:
👉 IOC = Factory schema + XML + reflection
👉 And DI, AOP, transactions, and so on are also very intuitive in XML
Although we now mostly use this annotation instead, the principle is basically the same 🐖
Annotations are easy to use, but it is recommended to learn through XML first, after all, structured documents have a sense of hierarchy, can leave a deeper impression ~ 😄
A little Spring
Condense Spring and you get this little thing 🐖
On second thought, we use Spring, and one of the main things we use it to help us manage and create this Bean.
So start at the source — where did the Bean come from (@_ @;)
Bean parsing process
As shown in the figure, we parse our XML files or annotations using the parser, wrap this information in a BeanDefinition class, and register it via the BeanDefinitionRegistry interface. Put beanDefinitionMap variables, key: beanName, value: BeanDefinition.
So just look at the property PI in BeanDefinition
BeanDefinition
- BeanClass: type of bean, used for instantiation 🐖
- Scope: Singleton, prototype
- IsLazy: lazy load, true generated on getBean, scope prototype invalid, false generated directly during Spring startup
- InitMethodName: Initializes the method, of course, by calling 🐖 on initialization
- Primary: Primary, used when there are multiple beans
- DependsOn: the dependent Bean can be created only after the dependent Bean is created
PS: @Component, @Bean, both parse to BeanDefinition
reflection
After we have the raw materials, we will take a look at the BeanFactory
How does the factory create this Bean?
Yes, it must be this reflex 😄
So, combining beanClass, one of the most important properties we get from the raw material, we can draw a picture 👇
So let’s look at this BeanFactory speaker 😄
BeanFactory
Let’s take a look at the methods provided by the BeanFactory, the root interface of the IOC container 👇
Mainly the getBean method, alias fetching, type fetching, and other judgment methods such as singletons, multicases, type matching, and containing beans
Let’s take a quick look at what its subinterfaces are
Here is a tip to share, pa 🐖
Look at the source code, is generally see the default interface directly Such as the DefaultListableBeanFactory here
Basically see a class name to know about the role, so the first to enter 👇
ListableBeanFactory
👉 traverse beans
HierarchicalBeanFactory
👉 provides parent-child relationship and can obtain the upper level BeanFactory
ConfigurableBeanFactory
👉 implements SingletonBeanRegistry, which is mainly the registration of singleton beans, generated
AutowireCapableBeanFactory
👉 has something to do with automatic assembly
AbstractBeanFactory
👉 singleton caching, and FactoryBean related
ConfigurableListableBeanFactory
👉 preinstantiate singleton beans, analyze, and modify BeanDefinition
AbstractAutowireCapableBeanFactory
👉 Create beans, inject properties, instantiate, call initialization methods, and so on
DefaultListableBeanFactory
👉 supports singleton beans, Bean aliases, parent-child BeanFactories, Bean type conversions, Bean postprocessing, factoryBeans, auto-assembly, and more
Is not very rich 😄
FactoryBean
A FactoryBean, which is itself a Bean, is a small factory, managed by a big factory called the BeanFactory.
You can see that it only has three methods
- GetObject () gets the object
- IsSingleton () singleton object
- GetObjectType () returns the type of the Bean object
Much less than the big factory BeanFactory, there is no strict Bean lifecycle process
😄 is described in 👉, a three-minute Quick Look at Factory Patterns in Spring
The FacotryBean object is itself a Bean, a small factory that can produce additional beans
BeanFactory, the root interface of the Spring container, is a large factory that produces a wide variety of beans
BeanName is the normal object
“&” +beanName gets the FacotryBean factory object that implements the interface
Roughly as follows 👇
ApplicationContext
Let’s look at the ApplicationContext again
As you can see, it extends many functions. Besides BeanFactory, it can also create and get beans, handle internationalization, events, get resources, and so on
- EnvironmentCapable The ability to get environment variables, including operating system variables and JVM environment variables
- ListableBeanFactory obtains all BeanNames, determines whether a BeanName has a BeanDefinition object, counts BeanDefinition objects, and obtains all BeanNames corresponding to a type
- HierarchicalBeanFactory Takes the parent BeanFactory and determines whether a name has the functionality of a bean object
- MessageSource Internationalization function to retrieve an internationalized resource
- ApplicationEventPublisher event publishing functions (key)
- ResourcePatternResolver loads and obtains resources, such as files, images, and other URL resources
There are also three important classes 👇, which I will not introduce in detail 😄
- ClassPathXmlApplicationContext
- AnnotationConfigApplicationContext
- FileSystemXmlApplicationContext
Take a look at the core!
The IOC container
Of course, it must be IOC.
We all know IOC is inversion of control, but don’t forget the word container, such as the container’s root interface BeanFactory, and the container’s implementation 👇
- ClassPathXmlApplicationContext
- AnnotationConfigApplicationContext
- FileSystemXmlApplicationContext
Also note the ubiquitous rear processor xxxPostProcessor 🐷
This is why Spring is so extensible!
We can apply these PostProcessors in various processes to extend, modify Bean definition information, and so on
You can see that in this container, the Bean is initialized, and there are more details about this process, please go to 👇
DI will be introduced at 🐷 when writing the property population
BeanFactory post-processor
BeanFactory, as the root interface of IOC container, has very high scalability. For example, when the raw material BeanDefinition is acquired at the very beginning, there are two post-processors for the BeanFactory factory 👇
BeanDefinitionRegistryPostProcessor
Through this interface, we can control our raw materials by ourselves. Add, delete, or obtain our BeanDefinitionDefinition through the BeanDefinitionRegistry interface
BeanFactoryPostProcessor
Through this interface, you can modify BeanDefinition, freeze, pre-instantiate singleton beans, and so on before instantiating the object
After all these obstacles, we finally get to the target method getBean, put the raw material into production, and finally get the Bean objects out
What follows is the life cycle of the Bean 😄
Bean life cycle
There is a standardized process for creating and managing beans!
This is clearly written in our BeanFactory 👇
A total of 14 steps, is not all of a sudden much clearer 😄
When looking at the source code in this section, pay attention to the two English words 😝
- Instantiate 👉 Instantiation
- Initialize 👉 Initialization
Ps: don’t look at the quick mistake ha ha 😝
If you look closely at the 14 steps above, you will find that the first 8 are Aware interfaces, and their function is very simple: to get the prefix xx 😄 of the word xxAware
For example, we in the above 👉 quick-and-dirty “three minutes Spring event mechanism” referred to in the event the publishing ApplicationEventPublisher, as long as you achieve ApplicationEventPublisherAware interface, Can get event publishing ApplicationEventPublisher!
Bean post-processor
During the instantiation and initialization process, place the Bean’s post-processor, BeanPostProcessor, and you get 👇
Here pay attention to the instantiation of extension points InstantiationAwareBeanPostProcessor, initialize the extension point BeanPostProcessor will be very much, we mainly to watch the AOP
AOP
So at what step does AOP proxy objects? 👇
You can see 👇 in the AbstractautoXyCreator class
Additional details are covered below in the AOP plug-in article 😄
For information about agency, see here
👉 the Java Proxy Pattern and the Quest for bytecode
👉 Cglib Dynamic Proxy Exploration (ASM, Spring)
conclusion
This article will be introduced here 🐖
This article mainly introduces the context of Spring, which is convenient for friends to have an overall impression of it
Take a look at some of these extension points, such as BeanFactoryPostprocessor, which starts with the source material, to BeanPostprocessor, which starts with the production Bean.
Instantiation, the order of initialization, the Bean lifecycle, and the functionality of the BeanFactory and subclass extensions to the ApplicationContext.
Then there’s the core mechanism: factory +XML+ reflection, and where AOP comes in, which leads to the rest of this article.
This issue’s mind map will be shared with subsequent chapters at 😋
Ps: recently busy up, more text speed will be slower oh ~, like if you can star standard oh, so you can receive more text message for the first time 🐷
I’m 4ye. We should… next time. See you soon!! 😆
If you like, you can also pay attention to the public number Java4ye support 4YE 😝