Interview talk: understand the principles of Springboot start

SpringBoot: Operating principle Inquiry

Speaking from pom.xml springboot we did not like javaweb, ssm as a tomcat to perform, because it embeds itself on a tomcat
and then we only need to configure the dependent pom.xml inside
Here Insert Picture Description
into the parent project, here is the real application management SpringBoot All versions of which depend on the place, SpringBoot version control center;
later we import dependence is no need to write the default version; however, if the imported package is not managed in dependence on the need to manually configure version;

Launcher

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

pringboot-boot-starter: the scene is springboot starter
springboot start essential, here we need web functionality to import starter-web

Startup class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/@SpringBootApplication 来标注一个主程序类 , 说明这是一个Spring Boot应用
@SpringBootApplication
public class SpringbootDemo02Application {
     public static void main(String[] args) {
        //将Spring应用启动起来
        SpringApplication.run(SpringbootDemo02Application.class, args);
    }
}

First, we learned people know, do not know it does not matter, why start class called startup class, and we ourselves can also write start classes, but if you create a Springboot project then will be built one and here the main difference is that * * @ SpringBootApplication **

@SpringBootApplication : SpringBoot application marked on a class == Description This class is SpringBoot main configuration class ==, SpringBoot you should run the main method of this class to start SpringBoot application; enter this comment:
Entered SpringBootApplicationI found three more important notes other notes if you do not know if I have another offer to go to a foundation to explain: Notes and reflection

@ComponentScan

The elements corresponding to annotation xml configuration function is to automatically scan and load match type of bean, bean definition is loaded to the IOC container

@SpringBootConfiguration

SpringBoot的配置类 ;标注在某个类上 , 表示这是一个SpringBoot的配置类;我们继续进去这个注解查看
Here Insert Picture Description
@Configuration:配置类上来标注这个注解 ,配置类-----配置文件;我们继续点进去,发现配置类也是容器中的一个组件。@Component
Here Insert Picture Description

这就说明了SpringbootConfiguration其实就是为了标注这个类是个组件,那我们接下来看第三个注解

@EnableAutoConfiguration

以前我们需要配置的东西,SpringBoot可以自动帮我们配置 ; @EnableAutoConfiguration告诉SpringBoot开启自动配置功能,这样自动配置才能生效;
Here Insert Picture Description但是到底自动配置啥,我们任然不知道所以继续点进去看

Here Insert Picture Description
@AutoConfigurationPackage自动配置包 , 点进去看到一个 @Import({Registrar.class})
Here Insert Picture Description
@import :Spring底层注解@import , 给容器中导入一个组件 ,导入的组件由 {Registrar.class} 将主配置类 【即**@SpringBootApplication**标注的类】的所在包及包下面所有子包里面的所有组件扫描到Spring容器= ;

然后回来继续看:@Import({AutoConfigurationImportSelector.class}) :给容器导入组件 ;

AutoConfigurationImportSelector类

导入哪些组件的选择器 ;
它将所有需要导入的组件以全类名的方式返回 , 这些组件就会被添加到容器中
它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ;

/AutoConfigurationImportSelector中获取配置文件信息;
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
   List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
         getBeanClassLoader());
   Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
         + "are using a custom packaging, make sure that file is correct.");
   return configurations;
}

SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguation.class,classLoader);
这里的getSpringFactoriesLoaderFactoryClass()方法返回的就是我们最开始看的启动自动导入配置文件的注解类;
Here Insert Picture Description
进入 SpringFactoriesLoader 查看
Here Insert Picture Description

找到对应的方法:发现最终会去读取一个配置文件 : META-INF/Spring.factories 的文件 。
我们打开spring.factories的配置文件 , 看到了很多自动配置的文件;这就是自动配置根源所在!
Here Insert Picture Description

结论

SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入容器 , 自动配置类就生效 , 帮我们进行自动配置工作;

SpringApplication.run分析

分析该方法主要分两部分,一部分是SpringApplication的实例化,二是run方法的执行;

SpringApplication的实例化

  1. 推断应用的类型是普通的项目还是Web项目
  2. 查找并加载所有可用初始化器 , 设置到initializers属性中
  3. 找出所有的应用程序监听器,设置到listeners属性中
  4. 推断并设置main方法的定义类,找到运行的主类

Here Insert Picture Description

Published 58 original articles · won praise 11 · views 2212

Guess you like

Origin blog.csdn.net/jiohfgj/article/details/104472570