SpringBoot

2021/9/9 6:06:00

本文主要是介绍SpringBoot,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • SpringBoot
    • 原理初探

SpringBoot

原理初探

自动配置:

pom.xml

  • spring-boot-starter-parent:核心依赖在父工程中!
  • 我们在写或者引入一些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 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;
}


这篇关于SpringBoot的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程