springboot的核心功能是自动装配
pom.xml
spring-boot-dependencies: 核心依赖在父工程中
在引入一些springboot依赖的时候可以不注明版本,在springboot中已经有这些依赖的版本仓库
启动器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-?</artifactId> </dependency>
可以自动导入?环境所有的相关依赖
springboot会将所有的功能场景,变成一个个启动器
开发者需要用到什么功能,只需找到对应的启动器starter即可
@SpringBootApplication
@SpringBootConfiguration: SpringBoot的配置
@Configuration: Spring配置类
@Component: Spring组件
@EnableAutoConfiguration
@AutoConfiguration: 自动配置包
@Import(AutoConfigurationPackages.Registrar.class):导入“包注册”器
@Import(AutoConfigurationImportSelector.class):导入选择器
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes) : 获取所有候选配置
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;
}
META-INF/spring.factories自动配置的核心文件
在org.springframework.bootspring-boot-autoconfiguration中
Properties properties = PropertiesLoaderUtils.loadProperties(resource); 所有的资源加载到配置类中
其中并不是所有的配置都会生效,经过遍历后,满足注解@ConditionOnXXX的才会产生对应的配置效果。
总结:Springboot所有的自动配置走在启动的时候经过@SpringBootApplication注解扫描META-INF/spring.factories文件并加载。只要在pom.xml中导入对应的start,就可以激发对应的启动器,完成自动装配。
1、springboot在启动的时候,从类路径下META-INF/spring.factories获取指定的值;
2、将这些自动装配的类导入容器,自动装配就会生效,帮助进行自动配置;
3、详见org.springframework.bootspring-boot-autoconfiguration包中,它将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中;
4、自动导入这个场景所需要的所有组件,并配置成功。
原文:https://www.cnblogs.com/amazinghcc/p/12977904.html