pom.xml
- spring-boot-dependencies:核心依赖在父工程中!
- 我们在写或者引入一些SpringBoot依赖的时候,不需要指定版本,就是因为有这些版本仓库
启动器:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
-
启动器:说白了就是Springboot的启动场景;
-
比如spring-boot-starter-web,他就会帮我们自动导入web环境所有的依赖!
-
springboot会将所有的功能场景,都变成一个个的启动器
-
我们要使用什么功能,就只需要找到对应的启动器就可以了 ‘starter’
主程序:
// @SpringBootApplication:标注这个类是一个Springboot的应用:启动类下的所有资源被导入
@SpringBootApplication
public class HelloworldApplication {
//SpringApplication
public static void main(String[] args) {
// 将springboot应用启动
SpringApplication.run(HelloworldApplication.class, args);
}
}
-
注解:@SpringBootApplication
@SpringBootConfiguration : springboot 的配置 @Configuration spring配置类 @Component:说明这也是一个spring的组件 @EnableAutoConfiguration :自动配置 @AutoConfigurationPackage : 自动配置包 @Import({Registrar.class}) :自动配置‘包注册’ @Import({AutoConfigurationImportSelector.class}) :自动配置导入选择 // 获取所有的配置 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 : 自动配置的核心文件
![](https://img.haomeiwen.com/i25773466/5ef02f3d00570b66.png)
![](https://img.haomeiwen.com/i25773466/0d1a4100c5d4e1d3.png)
Properties properties = PropertiesLoaderUtils.loadProperties(resource); // 所有的资源加载到配置类中
![](https://img.haomeiwen.com/i25773466/b6ffcc067564fb50.png)
结论:springboot所有的自动配置都是在启动的时候扫描并加载:spring.factories 所有的自动配置类都在这里面。但是不一定生效,要判断条件是否成立,只要在pom.xml中导入了对应的start,就有了对应的启动器了。有了启动器,我们自动装配就会生效,然后就配置成功了!
-
1.springboot 在启动的时候,从类路经下 META-INF/spring.factories 获取指定的值;
-
2.将这些自动配置的类导入容器,自动配置就会生效,帮我们进行自动配置!
-
3.以前我们需要自动配置的东西,现在springboot都帮我们做了!
-
4.整合javaEE,解决方案和自动配置的东西都在 spring-boot-autoconfigure-2.3.4.RELEASE.jar 这个包下
-
5.它会将所有需要导入的组件,以类名的方式返回,这些组件就会被添加到容器;
-
6.容器中也会存在非常多的xxxAutoConfiguration的文件(@Bean),就是这些类给容器中导入了这个场景需要的所有组件;并自动装配,@Configuration = JavaConfig
-
7.有了自动装配类,免去了我们手动编写配置文件的工作!
网友评论