1.SpringBoot的特点
依赖管理
- 父项目做依赖管理
<!--引入父工程,使用SpringBoot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</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、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
3、SpringBoot所有支持的场景
https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
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、引入非版本仲裁的jar,要写版本号 -
导入父项目后其他依赖无需版本号
-
父项目几乎声明了开发中用到的版本号(父项目中属性规定了对用jar的版本号)
-
可以修改默认版本号:
1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
2、在当前项目里面重写配置
<properties>
<mysql.version>5.1.43</mysql.version>
</properties>
<dependencys>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencys>
当需要修改版本号时在pom.xml中使用properties标签修改对应jar对应的属性版本号
自动配置
- 默认的包结构
• 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来(如conreoller)
• 无需以前的包扫描配置(如spring配置文件中配置包的扫描路径)
如:
<!-- 扫描Service -->
<context:component-scan base-package="base.service" />
• 改变扫描路径:
1 想要改变扫描路径,@SpringBootApplication(scanBasePackages=“com.atguigu”)
2 或者@ComponentScan 指定扫描路径
@SpringBootApplication
等同于=
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("指定扫描的包路径")
- 自动配好Tomcat
• 引入Tomcat依赖。
• 配置Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
-
自动配好SpringMVC
• 引入SpringMVC全套组件
• 自动配好SpringMVC常用组件(功能) -
自动配好Web常见功能,如:字符编码问题
• SpringBoot帮我们配置好了所有web开发的常见场景 -
各种配置拥有默认值
• 默认配置最终都是映射到某个类上,如:MultipartProperties
• 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象 -
按需加载所有自动配置项
• 非常多的starter场景启动器
• 引入了哪些场景这个场景的自动配置才会开启
• SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面