springboot 解读01->运行原理初探

源码解读

springboot 项目启动类:Springboot01Application

@SpringBootApplication
    public class Springboot01Application {
    
    
        public static void main(String[] args) {
    
    
            //将springboot应用启动
            SpringApplication.run(Springboot01Application.class, args);
        }
    }

知识点:

功能实现:

若在启动程序中禁用安全配置的自动配置功能,如下:

@EnableAutoConfiguration(exclude = {
    
    org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
注解:
    @SpringBootApplication
        1.@SpringBootApplication 标注这是一个springboot应用 ,启动类下的所有资源被导入
        2.@SpringBootConfiguration  springboot配置
            @Configuration  spring 配置类
            @Component  说明这也是一个spring 组件
        3.@EnableAutoConfiguration  自动配置
            @AutoConfigurationPackage  自动配置包
                @Import({Registrar.class}) 自动配置  包注册
            @Import({AutoConfigurationImportSelector.class})  自动配置 导入选择
方法:

解读方法:getCandidateConfigurations 获取候选的配置

List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    
    
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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;
    }
文件:

META-INF/spring.factories 自动配置的核心文件
在这里插入图片描述

配置文件:
在这里插入图片描述

 //所有资源加载到配置类中
 Properties properties = PropertiesLoaderUtils.loadProperties(resource);

在这里插入图片描述

思路:

1:springboot子啊启动的时候,从类路径下 META-INF/spring.factories 获取指定的值;
2:将这些自动配置的类导入容器,自动配置就会生效,帮我们进行自动配置;
3:以前我们需要自动配置的东西,现在springboot帮我们做了;
4:整合javaEE,解决方案和自动配置的东西都在spring-boot-autoconfigure-2.4.3.jar这个包下;
5:它会把所有需要导入的组件,以类名的方式返回,这些组件就会被添加到容器
6:容器中也会存在很多的xxxAutoConfiguration的文件(@Bean),就是这些类给容器中导入了这个场景所需要的所有组件,并自动配置。@Configuration ,javaConfig !
7:有了自动配置类,就省去了我们编写配置文件的工作。

结论:

springboot 所有自动配置都是在启动时扫描并加载:META-INF/spring.factories
所有的自动配置类都在这里面,但是不一定生效,要判断条件是否成立。只要导入了对应的start,就有对应的启动器了。有了启动器,我们自动装配就会生效,然后就配置成功!

不简单的方法

我最初以为就是运行了一个main方法,没想到却开启了一个服务;

@SpringBootApplication
public class SpringbootApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

SpringApplication.run分析

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

SpringApplication 这个类主要做了以下四件事情:
1、推断应用的类型是普通的项目还是Web项目
2、查找并加载所有可用初始化器 ,设置到initializers属性中
3、找出所有的应用程序监听器,设置到listeners属性中
4、推断并设置main方法的定义类,找到运行的主类

查看构造器:

public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
    
    
    // ......
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    this.setInitializers(this.getSpringFactoriesInstances();
    this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = this.deduceMainApplicationClass();
}

run方法流程分析:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40879055/article/details/114959899