SpringBoot自动装配
SpringBoot自动装配
启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
- 启动器就是SpringBoot的启动场景
- 比如spring-boot-starter-web,SpringBoot就会自动导入web环境所有的依赖。
- SpringBoot会将所有的功能场景都变成一个个的启动器。
主程序
//@SpringBootApplication 标准这个类是一个SpringBoot应用
@SpringBootApplication
@MapperScan("com.wk.poem.mapper")
public class DemoApplication {
public static void main(String[] args) {
//将SpringBoot应用启动
SpringApplication.run(DemoApplication.class, args);
}
}
注解
@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 = new ArrayList(SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()));
ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).forEach(configurations::add);
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
具体流程图:https://www.processon.com/view/link/62ff92ae1efad47d12435e63
密码:3l9P
结论:SpringBoot所有的自动配置都是在启动的时候扫描加载:spring.factories所有的自动配置类都在这里。