SpringBoot原理

ⅰ亾dé卋堺 提交于 2020-12-25 20:02:46
本文标识 : jsbt0002                  
本文编辑 :  Jack 风                  
编程工具 : IDEA                     
阅读时长 : 8分钟  


这部分内容入门阶段可以跳过,有基础的可以直接看下一篇

1、POM文件

父项目

  
    
  
  
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>2.1.1.RELEASE</version>

  5. <relativePath/> <!-- lookup parent from repository -->

  6. </parent>

他的父项目是:

  
    
  
  
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-dependencies</artifactId>

  4. <version>2.1.1.RELEASE</version>

  5. <relativePath>../../spring-boot-dependencies</relativePath>

  6. </parent>

这是真正管理Spring Boot应用里面所依赖的版本

Spring Boot的版本仲裁中心;

以后我们导入依赖默认是不需要写版本;

(没有在dependencies里面管理的依赖自然需要声明版本号)

2、启动器

  
    
  
  
  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-web</artifactId>

  4. </dependency>

spring-boot-starter-web

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

点击进去可以看到帮我们引入很多web相关的依赖

Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器

3、主程序类,主入口类

  
    
  
  
  1. @SpringBootApplication

  2. public class DemoApplication {


  3. public static void main(String[] args) {

  4. SpringApplication.run(DemoApplication.class, args);

  5. }


  6. }

@SpringBootApplication : Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

  
    
  
  
  1. @Target(ElementType.TYPE)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. @Inherited

  5. @SpringBootConfiguration

  6. @EnableAutoConfiguration

  7. @ComponentScan(excludeFilters = {

  8. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

  9. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

  10. public @interface SpringBootApplication {

  • @SpringBootConfiguration : Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类

  • @Configuration : 配置类上来标注这个注解,配置类也是容器中的一个组件@Component

  • @EnableAutoConfiguration:开启自动配置功能

  
    
  
  
  1. @AutoConfigurationPackage

  2. @Import(EnableAutoConfigurationImportSelector.class)

  3. public @interface EnableAutoConfiguration {

  • 将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;

  • 调用了 SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)

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

  • 以前我们需要自己配置的东西,自动配置类都帮我们;

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

  • J2EE的整体整合解决方案和自动配置都在 spring-boot-autoconfigure-1.5.9.RELEASE.jar

自动配置原理

1)、SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration

2)、@EnableAutoConfiguration 作用:

  • 可以查看selectImports()方法的内容;

  • 利用EnableAutoConfigurationImportSelector给容器中导入一些组件?

  • List<String>configurations=getCandidateConfigurations(annotationMetadata,attributes);获取候选的配置

  • javaSpringFactoriesLoader.loadFactoryNames()扫描所有jar包类路径下META-INF/spring.factories把扫描到的这些文件的内容包装成properties对象properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中

将类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中

精髓:

 1)、SpringBoot启动会加载大量的自动配置类

 2)、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;

 3)、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)

 4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这些属性的值;

  
    
  
  
  1. xxxxAutoConfigurartion:自动配置类(给容器中添加组件);

  2. xxxxProperties:封装配置文件中相关属性;



本文分享自微信公众号 - DataScience(DataScienceTeam)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!