Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the underlying principle of Spring Boot?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the underlying principle of Spring Boot". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the underlying principle of Spring Boot".

1. Based on your understanding of springboot, describe what springboot is.

It is a framework that serves the spring framework, can simplify configuration files, quickly build web applications, built-in tomcat, do not need to be packaged and deployed, run directly.

two。 What does convention over configuration mean?

Maven directory structure a) default resources folder storage configuration file b) default packaging method: jarspring-boot-starter-web contains spring mvc-related dependencies and built-in tomcat containers by default, which makes it easier to build a web application. By default, application.properties/yml files are provided by default through the spring.profiles.active attribute to determine the configuration file read when running the environment. EnableAutoConfiguration is automatically for dependent starter by default.

Which annotations are made up of 3.@SpringBootApplication, and what role do they represent?

SpringBootApplication is essentially made up of three annotations, namely

@ Configuration@EnableAutoConfiguration@ComponentScan@Configuration: @ Configuration is marked in the startup class, which means that it is also a configuration class of the IoC container

The @ EnableAutoConfiguration:springboot application loads all eligible @ Configuration configurations into the IoC container currently created and used by SpringBoot.

@ ComponentScan:ComponentScan scans all classes under the current package with @ Component, @ Repository, @ Service, @ Controller to the IoC container by default.

The realization principle of 4.springboot automatic Assembly

If you used redis in previous spring to define bean in xml, now you only need to rely on a jar package of spring-boot-starter-data-redis. RedisConfiguration is defined in jar. Spring automatically loads RedisConfiguration when it starts, then how does spring know where the configuration class is? the path of the RedisConfiguration class is placed in the classpath*META-INF/spring.factories file, and spring will load the configuration configured in this file.

(1) SpringApplication.run (AppConfig.class,args); there is refreshContext (context) in the execution process.

(2) refreshContext (context); internally parses the tags on our configuration class. Note @ EnableAutoConfiguration to realize automatic assembly function

(3) the configuration class introduced by @ Import in the @ EnableAutoConfiguration annotation will be parsed. AutoConfigurationImportSelector

(4) there is this method in the class AutoConfigurationImportSelector. Spring factoriesLoader.loadFactoryNames (getSpringFactoriesLoaderFactoryClass (), getBeanClassLoader ())

(5) the function of SpringFactoriesLoader.loadFactoryNames is to read the META-INF/spring.factories file in the jar package / project.

(6) spring.factories is configured with Configuration classes to be automatically assembled

What is the principle of the spi mechanism in 5.spring?

The full name of SPI is Service Provider Interface, which looks for the mechanism of service implementation for an interface. When the service provider provides an implementation of the service interface, a file named after the service interface is also created in the META-INF/services/ directory of the jar package. In this file is the specific implementation class that implements the service interface. When the external program assembles this module, it can find the specific implementation class name through the configuration file in the jar package META-INF/services/, and load the instantiation to complete the injection of the module. With this convention, there is no need to put the service in the code, and the service class can be found when the module is assembled.

In the auto-assembly process of springboot, the META-INF/spring.factories file is eventually loaded, and the loading process is loaded by SpringFactoriesLoader. Search for all META-INF/spring.factories configuration files from each Jar package under CLASSPATH, then parse the properties file, find the configuration with the specified name and return. It should be noted that, in fact, it will not only look under the ClassPath path, but also scan the Jar package under all paths, but this file will only be found in the jar package under Classpath.

The starter can be customized through spi technology, and the implementation class can be added to the custom configuration file META-INF/spring.factories. Projects that depend on this starter will scan the configuration under the jar package and find the implementation class to load and instantiate.

Thank you for your reading, the above is the content of "what is the underlying principle of Spring Boot". After the study of this article, I believe you have a deeper understanding of what the underlying principle of Spring Boot is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report