springboot 源码初读(Initial reading of springboot source code)

启动文件

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

注解

1@SpringBootApplication
    2@SpringBootConfiguration : springboot的配置
      3@Configuration :spring的配置类
       4 @Component:说明这也是spring的组件
    2@EnableAutoConfiguration :自动配置
      3@AutoConfigurationPackage: 自动配置包
        4@Import({Registrar.class}): 自动配置‘包注册’
      3@Import({AutoConfigurationImportSelector.class}):自动导入选择
         4  List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);  :获取所有配置
         4 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;     :获取候选配置
    2@ComponentScan :

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

  • springboot在启动的时候,从路径下/META-INF/spring.factories获取指定的值:
  • 将这些自动装配的类导入容器,自动装配就会生效,帮我进行自动配置!
  • 以前我们需要自动配置的东西,现在springboot帮我们做了
  • 整合javaEE,解决方案和自动配置的东西都在spring-boot-autoconfigure-2.2.0RELEAES.jar这个包下
  • 他会把所有需要导入的组件,以类名的方式返回,这些组件就会被添加到容器
    6.容器中也会存在非常多的xxxAutoConfiguration的文件(@Bean),就是这些类给容器导入了这个场景需要的所有组件
————————

Startup file

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

annotation

1@SpringBootApplication
    2@SpringBootConfiguration : springboot的配置
      3@Configuration :spring的配置类
       4 @Component:说明这也是spring的组件
    2@EnableAutoConfiguration :自动配置
      3@AutoConfigurationPackage: 自动配置包
        4@Import({Registrar.class}): 自动配置‘包注册’
      3@Import({AutoConfigurationImportSelector.class}):自动导入选择
         4  List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);  :获取所有配置
         4 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;     :获取候选配置
    2@ComponentScan :

Summary: all automatic configurations of spring boot are scanned and loaded at startup: spring All automatic assembly classes of factories are here. However, it does not necessarily take effect. It is necessary to judge whether the conditions are true. As long as the corresponding start is imported, there will be a corresponding starter. With the starter, our automatic assembly will take effect, and the configuration will be successful!

  • springboot在启动的时候,从路径下/META-INF/spring.factories获取指定的值:
  • These containers will be automatically configured and automatically assembled for me!
  • We used to need to configure things automatically, but now springboot helps us do it
  • 整合javaEE,解决方案和自动配置的东西都在spring-boot-autoconfigure-2.2.0RELEAES.jar这个包下
  • He will return all the components to be imported in the form of class name, and these components will be added to the container
    6. There will also be many xxxautoconfiguration files (@ bean) in the container. These classes import all the components required by this scenario into the container