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/03 Report--
1 @ SpringBootApplication comment
The startup class of SpringBoot, the entry class, needs to be annotated with the @ SpringBootApplication annotation. In the startup class, our main method is the entry method of the Java application.
@ SpringBootApplication is a combined annotation. The specific source codes are as follows:
The three most important annotations are @ SpringBootConfiguration, @ EnableAutoConfiguration, and @ ComponentScan.
2 @ SpringBootConfiguration Annotation
It is mainly responsible for annotations related to the configuration of Spring Boot applications. It is also a combined annotation. The specific source codes are as follows:
From the source code, you can see that it also uses the @ Configuration annotation. Both of them mark the current class as a configuration class, and can inject the instance corresponding to the method marked with the @ Bean annotation into the Spring container, and the instance name is the method name.
In addition, in the @ Configuration annotation source code, we also see a @ Component annotation, which is encapsulated again, mainly to instantiate the ordinary POJO into the Spring container. The specific source codes are as follows:
Therefore, it is recommended that you use @ SpringBootConfiguration in SpringBoot applications.
3 @ EnableAutoConfiguration comment
Mainly used to start automatic configuration, Spring Boot can automatically implement the relevant configuration of the application according to the dependency information, which is divided into two parts: one is to collect all the EnableAutoConfiguration-related bean classes in spring.factories, and the other is to register the resulting classes into the Spring container. Load all the matching configurations into the IoC container. The specific source codes are as follows:
Component invocation diagram, as follows:
How to understand this picture? In fact, it involves the creation of BeanFactory. In the Spring framework, the refresh method of ApplicationContext is called to start the Spring container, then a BeanFactory is created, various packages are scanned, classes annotated with @ Configuration, @ Import, @ SpringBootApplication, and so on are read, and then BeanDefinition is generated and finally registered in BeanFactory.
It is then left to BeanFactoryPostProcessor to execute, and the BeanFactory post processor handles the BeanDefinition. For example, in the BeanFactoryPostProcessor interface, the postProcessBeanFactory method is provided to receive the ConfigurableListableBeanFactory object for processing. The specific source codes are as follows:
Other configuration annotations such as @ Configuration will be handled by ConfigurationClassPostProcessor.
The above ConfigurationClassPostProcessor is mainly the implementation class of the BeanFactoryPostProcessor interface. The main purpose is to get all the BeanDefinition lists from BeanFactory, traverse the corresponding BeanDefintion of those classes annotated with @ Configuration, @ Import and other configuration properties, and then register. The specific source codes are as follows:
Specifically, we can also see how its parse method is handled, which parses annotations.
When you see the final deferredImportSelectorHandler, there is a deferredImportSelectors collection in this inner class, which is mainly used to add AutoConfigurationImportSelector. This internal private class mainly maintains a deferredImportSelectors list of type DeferredImportSelectorHolder. This last line of code calls the process method after processing the other BeanDefinitions.
Next, let's take a look at the process method, which is responsible for automatically configuring the internal implementation of class import. The specific source code is as follows:
This method needs to be understood in this way:
First, DeferredImportSelector it will find EnableAutoConfiguration as key in the META-INF/spring.factories file under the spring-boot-autoconfigure package path, and then get the corresponding list of automatic configuration classes.
In the second step, you can find the corresponding classes that need to be automatically configured through key. It then iterates through all the class names, loading and importing the corresponding configuration classes.
The general idea is to create a ConfigurationClass object that contains the current configuration class, pass it into the called doProcessConfigurationClass method, and then process the annotations that the class contains. If it is a @ Import annotation, it will be processed in the processImports method.
Specifically, those configuration classes that are not ImportSelector interface implementation classes and ImportBeanDefinitionRegistrar interface implementation classes will call the processConfigurationClass method to handle other annotations above the autoconfiguration class, and conditionally generate bean and register to the Spring container with all the methods annotated by @ Bean inside the autoconfiguration class, so that you can eventually provide the default implementation of specific functional components, that is, the automatic configuration function of SpringBoot is realized when you use it. For example, you can inject a functional component directly through the @ Autowried annotation without displaying the configuration.
The specific source codes are as follows (instead of posting all the source codes here, you can take a look at the comments given by it):
4 get Bean class information
We can look at this annotation to see how it loads the configuration. In the source code, you can see the @ Import ({AutoConfigurationImportSelector.class}) annotation, which imports the autoconfiguration selector.
The AutoConfigurationImportSelector selector is the implementation class of the DeferredImportSelector interface, which is executed in BeanFactory after all BeanDefinition processing to load and import the SpringBoot automatic configuration class, and decides whether to register the Bean defined internally in the configuration class with the Spring container based on the @ Conditional conditional configuration. The specific source codes are as follows:
In AutoConfigurationImportSelector.class, you can see that a selectImports method has been implemented to export the Configuration. The getAutoConfigurationEntry method is called in the method to get the bean class information. The specific source codes are as follows:
Let's move on to the getAutoConfigurationEntry method. The specific source code is as follows:
Then let's take a look at the called getCandidateConfigurations method, which mainly wants to get all the corresponding configurations, in which the loadFactoryNames method is called to load the spring.factories file. Their source codes are as follows:
The specific source code of the loadFactoryNames method is as follows:
Then in the loadSpringFactories method, find all the spring.factories configuration information and return it all. The specific source codes are as follows:
Source: nanotechnology developer community
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.