pom.xml
- spring-boot-dependencies:核心依赖在父工程中!
资源配置(已经配置好)
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
- 我们在写或引入spring依赖的时候 不需要指定版本,因为有版本仓库
启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 启动器:springboot的启动场景
- 例如:spring-boot-starter-web,会帮我们导入所有的web环境依赖
- springboot 会将所有功能场景变成启动器
- 我们要使用什么功能只需要找到对应的启动器
主程序
package com.example2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
}
此处是按目录顺序列出
- @SpringBootApplication:标注这个类是一个springboot的应用:启动类下的所有资源
SpringApplication.run(Demo2Application.class, args);
:将springboot启动- SpringBootApplication里面的其它注解
- @SpringBootConfiguration:springboot的配置
- @Configuration:spring配置类
- @Component:spring的注解
- @Configuration:spring配置类
- @SpringBootConfiguration:springboot的配置
- @EnableAutoConfiguration:自动配置
- @AutoConfigurationPackage:自动配置包
- @Import(AutoConfigurationPackages.Registrar.class):自动配置·包注册 ·
- @Import({AutoConfigurationImportSelector.class}):自动配置导入选择
获取所有配置
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
获取候选的配置
- @AutoConfigurationPackage:自动配置包
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:自动配置的核心文件
结论:
- SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值
- 将这些值作为自动配置类导入容器 , 自动配置类就生效 , 帮我们进行自动配置工作;
- 整个J2EE的整体解决方案和自动配置都在springboot-autoconfigure的jar包中;
- 它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ;
- 有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作;