自动配置:
pom.xml
启动器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
starter
主程序
//@SpringBootApplication:标注这个类是一个springboot的应用 @SpringBootApplication public class SpringBootDemoApplication { public static void main(String[] args) { //将springboot应用启动 SpringApplication.run(SpringBootDemoApplication.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 = 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; }