SpringBoot_学习复盘_0
SpringBoot 1
1.什么是spring
spring是为了解决企业级应用开发的复杂性而创建的,简化开发
2.什么是springboot
约定大于配置!!!
- maven
- spring
- springmvc
- springboot
- ...
优点:。。。
3.微服务:
什么是微服务
4.第一个springBoot程序
springboot核心:自动装配
springboot启动类本身就是spring组件
pom.xml
- 有一个父项目
- gav坐标
- 依赖:所有的springboot依赖都是使用:spring-boot-starter开头的
- web依赖包括:tomcat,dispatcherServlet,xml(如果在创建项目时不勾选web就没有这些)
- 单元测试
- 打jar包插件

原理初探
自动装配
- 启动器
- pom.xml
- 主程序
pom.xml:
- spring-boot-dependencies:核心依赖在父工程中
- 我们在写或引入springboot依赖的时候,不需要指定版本,因为有版本仓库
启动器
-
<!--启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> -
启动器:就是springboot的启动场景
-
比如:spring-boot-starter-web,他就会自动帮我们导入web环境需要的所有依赖
-
springboot会将所有的功能场景都变成一个个的启动器
-
我们要使用什么功能,就只需要找到对应的启动器就可以了
starter
主程序
- @SpringBootApplication:标注这个类是一个springboot的应用:启动类下的所有资源被导入(<—兜兜转转)
- SpringApplication.run:将springboot应用启动
注解:
-
@SpringBootConfiguration:springboot的配置
- @Configuration:spring配置类
- @Component:说明这也是一个Spring的组件
- @Configuration:spring配置类
-
@EnableAutoConfiguration:自动配置!!
-
@AutoConfigurationPackage:自动配置包
- Import(AutoConfigurationPackages.Registrar.class):自动配置
包注册
- Import(AutoConfigurationPackages.Registrar.class):自动配置
-
@Import(AutoConfigurationImportSelector.class):自动配置导入选择
-
//获取所有的配置 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); -
获取候选的配置:getCandidateConfigurations()
-
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应用启动:
SpringApplication类
这个类主要做了以下四件事情
- 推断应用类型是普通的项目还是web项目
- 查找并加载所有可用的初始化器,设置到initializers(初始化)属性中
- 找出所有的应用程序监听器,设置到listeners属性中
- 推断并设置main方法的定义类,找到运行的主类
run方法:开启了一个服务
核心:
javaConfig: @Configuration @Bean
面试可能会问到:关于springboot,谈谈你的理解
围绕两个点:
- 自动装配
- run():四步

浙公网安备 33010602011771号