In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about the underlying method of SpringBoot2 automatic configuration. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
1 automatic configuration underlying analysis 1.1 automatic configuration annotation parsing
The @ SpringBootApplication annotation is the core annotation of SpringBoot, which is composed of the following three annotations.
@ SpringBootConfiguration
There is a @ Configuration annotation at the bottom of the annotation, indicating that the current class is a configuration class
@ EnableAutoConfiguration this annotation is made up of the following two annotations
@ AutoConfigurationPackage: the underlying layer uses @ Import ({Registrar.class}) to register components in the container. Registrar.class gets the package where the main program is located and imports all the components under the package, which indirectly indicates the default package scanning rule of SpringBoot: the package in which the main program (MainApplication) is located and all the subpackages of this package will be scanned.
@ Import (AutoConfigurationImportSelector.class): there is a selectImports method under AutoConfigurationImportSelector.class, in which the getCandidateConfigurations method inside getAutoConfigurationEntry is used to obtain the components that need to be registered. The getCandidateConfigurations method uses getResources ("META-INF/spring.factories") in the loadSpringFactories method in the SpringFactoriesLoader.loadFactoryNames inner layer to scan the files under the META-INF/spring.factories location of all jar packages in the current system by default. Later, it was found that there was also a META-INF/spring.factories file in the spring-boot-autoconfigure-2.3.4.RELEASE.jar package, in which there was an EnableAutoConfiguratio configuration item that wrote all the configuration classes that spring-boot would load into the container as soon as it was started. Although all xxxxAutoConfiguration are loaded by default when automatic configuration is started in our 127scenarios, it is finally configured on demand according to the conditional assembly rule (@ Conditional).
The @ ComponentScan annotation defines the rules for package scanning
1.2 modify the default configuration
SpringBoot installs all the components at the bottom by default, but if the user configures it himself, the user has priority. If you use the @ ConditionalOnMissingBean annotation to determine whether the component exists in the container, configure it with the default class.
1.3 Summary
● SpringBoot first loads all automatic configuration classes through xxxxxAutoConfiguration
Every automatic configuration class in ● takes effect according to the condition. By default, the value specified in the configuration file is bound. This value is taken in xxxxProperties, and xxxProperties is bound to the configuration file.
The configuration class in effect of ● will assemble many components into the container.
As long as ● has these components in the container, it is equivalent to these functions.
● customized configuration
○ users directly replace the underlying components with their own @ Bean
○ uses configuration items to modify the default configuration information in the configuration file that is created. For example, configuring the character encoding format can be found in the comments in HttpEncodingAutoConfiguration
XxxxxAutoConfiguration-> components->
Get the value in xxxxProperties-> application.properties
1.4 Best practices
summarizes the best project practice flow of SpringBoot according to the above automatic configuration principle.
Introduce scene dependency
Org.springframework.boot spring-boot-starter-web
See which components are configured automatically
The automatic configuration corresponding to the default scenario generally takes effect.
Debug=true in the configuration file enables the automatic configuration report, in which Positive (effective component), Negative (ineffective component), and the reason why it does not take effect will be shown why it does not match to
Modify configuration item
Reference documentation: all configuration items that SpringBoot may use
Self-analysis according to the previous customized way
Component modification
@ Bean, @ Component. Notes
. Business proc
2 Development Tips 2.1 Lombok simplifies development
Step 1: import dependencies (since the version is already configured in the starter initiator, you don't have to set it again)
Org.projectlombok lombok
Step 2: search and install the lombok plug-in
Step 3: use annotations for development
The use of annotations in JavaBean annotations does not automatically generate code, but rather complements the annotated methods during compilation. And if you need to use a multi-parameter but incomplete constructor, no annotations can be done, and you need to handwrite on the source code.
@ NoArgsConstructor: no parameter constructor
@ AllArgsConstructor: full parameter constructor
@ ToString: full parameter toString method
@ Data: getter and setter methods
Generate log
Add @ Slf4j to the class
Log.info method to print logs
2.2 pseudo hot updates
Step 1: import dependencies
Org.springframework.boot spring-boot-devtools true
Step 2: use the hot update function to update the code in the project source code, do not need to restart the server, directly use Ctrl+F9 to refresh the project information, you can use the updated code. Static page changes use Ctrl+F9, which updates the page information directly in the server, and other information changes are equivalent to using buttons to restart the project. You can use JRebel if you want to achieve real-time updates without any action, but the drawback is charging.
2.3 create a Spring Initailizr project
The trick of is to create a springBoot project directly when using the enterprise version of IDEA. You can also customize the technologies needed in the project. After the creation is completed, the corresponding main program classes and pom.xml files of the SpringBoot project will be automatically generated.
When you right-click to create a project, select Spring Initailizr, mainly by checking the technology you need.
Effect:
These are all the contents of this article entitled "how to automatically configure the underlying layer of SpringBoot2". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.