您当前的位置: 首页 >  ar

white camel

暂无认证

  • 2浏览

    0关注

    442博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot——starter启动器、SpringBoot自动配置原理、@Conditional、run方法执行流程图

white camel 发布时间:2020-04-15 13:04:37 ,浏览量:2

目录
  • 一、pom.xml文件
    • 1、父依赖
    • 2、启动器 spring-boot-starter
  • 二、默认主启动类的分析原理(重点)
    • 1、@SpringBootApplication
    • 2、@ComponentScan
    • 3、@SpringBootConfiguration
    • 4、@EnableAutoConfiguration
    • 5、spring-factories
  • 三、分析自动配置原理 (接着上面继续)(重点)
    • 1、以HttpEncodingAutoConfiguration (Http编码自动配置) 为例解释自动配置原理;
    • 2、@Conditional派生注解
  • 四、SpringBoot自动配置原理流程( 结合二、三)(重点)
  • 五、SpringApplication主方法执行
    • 1、不简单的方法
    • 2、SpringApplication
    • 3、run方法流程图
在这里插入图片描述 一、pom.xml文件

跳转到目录 我们之前写的HelloSpringBoot,到底是怎么运行的呢,Maven项目,我们一般从pom.xml文件探究起;

1、父依赖

跳转到目录 其中它主要是依赖一个父项目,主要是管理项目的资源过滤及插件!


    org.springframework.boot
    spring-boot-starter-parent
    2.2.5.RELEASE
     

点进去,发现还有一个父依赖


  org.springframework.boot
  spring-boot-dependencies
  1.5.9.RELEASE
  ../../spring-boot-dependencies

这里才是真正管理SpringBoot应用里面所有依赖版本的地方,SpringBoot的版本控制中心;

以后我们导入依赖默认是不需要写版本号; 但是如果导入的包没有在依赖中管理着就需要手动配置版本了

2、启动器 spring-boot-starter

跳转到目录


    org.springframework.boot
    spring-boot-starter-web

  • springboot-boot-starter-xxx:就是spring-boot的场景启动器
  • spring-boot-starter-web:帮我们导入了web模块正常运行所依赖的组件;

SpringBoot将所有的功能场景都抽取出来,做成一个个的starter (启动器),只需要在项目中引入这些starter即可,所有 相关的依赖都会导入进来 , 我们要用什么功能就导入什么样的场景启动器即可 ;我们未来也可以自己自定义 starter;

二、默认主启动类

跳转到目录

/**
 * 程序的主入口
 */
// 本身就是一个Spirng的组件, 就是一个Spring的配置类
@SpringBootApplication
public class SpringBootApplication {

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

但是一个简单的启动类并不简单!!我们来分析一下这些注解都干了什么

1、@SpringBootApplication

跳转到目录 作用:标注在某个类上说明这个类是SpringBoot的主配置类 , SpringBoot就应该运行这个类的main方法来启动SpringBoot 应用;

进入这个注解:可以看到上面还有很多其他注解!

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
}
2、@ComponentScan

跳转到目录 这个注解在Spring中很重要 ,它对应XML配置中的元素。 作用:自动扫描并加载符合条件的组件或者bean ,将这个bean定义加载到IOC容器中

3、@SpringBootConfiguration

跳转到目录 作用:SpringBoot的配置类 ,标注在某个类上 , 表示这是一个SpringBoot的配置类;

我们继续进去这个注解查看

@Configuration
public @interface SpringBootConfiguration {
}
@Component
public @interface Configuration {
}
  • 这里的 @Configuration,说明这是一个配置类 ,配置类就是对应Spring的xml 配置文件;
  • 里面的 @Component 这就说明,启动类本身也是Spring中的一个组件而已,负责启动应用!
  • 我们回到 SpringBootApplication 注解中继续看。
4、@EnableAutoConfiguration

跳转到目录

  • @EnableAutoConfiguration :开启自动配置功能 以前我们需要自己配置的东西,而现在SpringBoot可以自动帮我们配置@EnableAutoConfiguration告诉SpringBoot开启 自动配置功能,这样自动配置才能生效;

点进注解继续查看:

  • @AutoConfigurationPackage :自动配置包
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
  • @import :Spring底层注解@import , 给容器中导入一个组件(将Registear导入容器) Registrar.class 作用:将主启动类的所在包(com.zy)及包下面所有子包里面的所有组件扫描到Spring容器 ; 在这里插入图片描述 这个分析完了,退到上一步,继续看

  • @Import({AutoConfigurationImportSelector.class}) :给容器导入组件; AutoConfigurationImportSelector :自动配置导入选择器,那么它会导入哪些组件的选择器呢?我们点击去这个类看源码:

在这里插入图片描述 将所有需要导入的组件以全类名的方式返回; 这些组件就被添加到容器中了, 也就是上图selectImports方法返回的String数组,该数组中存放的就是要被添加组件的全类名! 在这里插入图片描述 从这里就可以看出, 这个String[] 数组 返回了124 个自动配置类(xxxAutoConfiguration), 将这些以全类名的组件(这些组件就是各个场景所需要的)导入到容器中, 并配置好这些组件;

有了自动配置类, 就免去了我们手动编写配置注入功能组件等工作!

这些124个配置类, SpringBoot是从哪得到的呢? 看下面的getCandidateConfigurations方法

1、这个类中有一个这样的方法 getCandidateConfigurations

protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List 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;
}
  • this.getSpringFactoriesLoaderFactoryClass, 返回EnableAutoConfiguration字节码, 也就是说, 从properties中获取EnableAutoConfiguration.class类对应的值,然后把他们添加到容器中
protected Class getSpringFactoriesLoaderFactoryClass() {
        return EnableAutoConfiguration.class;
    }

2、这个方法又调用了 SpringFactoriesLoader 类的静态方法!我们进入SpringFactoriesLoader类loadFactoryNames()方法

public static List loadFactoryNames(Class factoryType, @Nullable ClassLoader classLoader) {
    String factoryTypeName = factoryType.getName();
    return (List)loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}

3、我们继续点击查看 loadSpringFactories 方法

private static Map loadSpringFactories(@Nullable ClassLoader classLoader) {
        MultiValueMap result = (MultiValueMap)cache.get(classLoader);
        if (result != null) {
            return result;
        } else {
            try {
                Enumeration urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
                LinkedMultiValueMap result = new LinkedMultiValueMap();

                while(urls.hasMoreElements()) {
                    URL url = (URL)urls.nextElement();
                    UrlResource resource = new UrlResource(url);
                    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                    Iterator var6 = properties.entrySet().iterator();

                    while(var6.hasNext()) {
                        Entry entry = (Entry)var6.next();
                        String factoryTypeName = ((String)entry.getKey()).trim();
                        String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
                        int var10 = var9.length;

                        for(int var11 = 0; var11             
关注
打赏
1661428283
查看更多评论
0.0448s