springboot之自动配置原理
依赖管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
几乎声明了所有开发中常用的依赖的版本号,自动仲裁版本
开发导入starter场景启动器。
1.见到很多的spring-boot-starter-* :*就是某种场景
2.只要引入stater,这个场景的所有常规需要的依赖我们都自动引入
3.springboot所有支持的场景
4.见到的*-spring-boot-starter,第三方为我们提供的简化开发的场景启动器。
5.所有场景启动器依赖,最底层的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
无需关注版本号,自动版本仲裁
1.引入依赖默认都可以不写版本号
2.自己引入的依赖都需要协商版本号
可以修改版本号
自动配置
自动装配tomcat
- 引入tomcat依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
- 配置tomcat
自动配置springmvc
- 引入springmvc全套组建
- 自动配好springmvc常用组件
自动配置web常见的功能,如字符编码问题
- springboot帮我们配置好了所有web开发的常见场景
默认的包结构
- 主程序所在的包,及其下面所有子包里面的组件都会被默认扫描出来
- 无需以前的包扫描配置
- 想要改变主包的扫描路径,@SpringBootApplication(scanBasePackages = "com.example")或者@ComponentScan指定扫描路径
- 一个SpringBootApplication注解相当于三个注解。
各种配置拥有的默认值
- 在application.properties中都存在默认值
- 默认配置最终都是映射到Multi partPorperties
- 配置文件的值最终会被绑定到每个类上,这个类会在容器中创建对象。
按需加载所有自动配置项
- 非常多的starter
- 引入了那些场景这个场景的自动配置才会开启
- Springboot所有的自动配置功能都在spring-boot-autoconfigure包里面
容器功能
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/articles/17051282.html

浙公网安备 33010602011771号