Spring Boot来简化Spring应用开发,约定大于配置。J2EE开发的一站式解决方案。
一、优点
- 快速创建独立运行的Spring项目以及与主流框架集成
- 使用嵌入式的Servlet容器,应用无需打成WAR包
- starters自动依赖与版本控制
- 大量的自动配置,简化开发,也可修改默认值
- 无需配置XML,无代码生成,开箱即用
- 准生产环境的运行时应用监控
- 与云计算的天然集成
二、创建Springboot 项目
使用 Spring Initializer快速创建项目
(二)、 项目探究
(1)pom文件
父项目:控制应用里面的所有依赖版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
启动器(spring-boot-starter- *): 帮我们导入了web模块和test模块正常运行所依赖的组件;
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些
starter相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器
<dependencies>
<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>
</dependencies>
简化部署:应用打成jar包,直接使用java -jar的命令进行执行;
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(2)主程序类,主入口类
@SpringBootApplication
public class Springboot03Application {
public static void main(String[] args) {
SpringApplication.run(Springboot03Application.class, args);
}
}
@SpringBootApplication: 标注在某个类上,说明这个类是SpringBoot的主配置类,SpringBoot就运行这个类的main方法来启动SpringBoot应用;
(3)resources
application.properties 配置文件
static 存放静态文件
templates 模板