Spring Boot简介:
- 简化spring应用开发的框架
- 整个spring技术栈的整合
- J2EE开发的一站式解决方案
优点
- 快速创建独立运行的spring项目
- 使用嵌入式的Servlet容器,应用无需打包
- starter自动依赖与版本控制
- 大量的自动配置,简化开发
- 无需配置XML,无代码生成,开箱即用
- 准生产环境的运行时应用监控
首先搭建一个Spring Boot的Hello World 来体验一下Spring Boot的强大。
我们以一个功能为例子:浏览器发送hello 请求,服务器接收请求被处理,返回Hello World字符串。
1.创建maven工程
注意:是创建工程,而不是module
1.2导入Spring Boot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.编写controller
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "Hello World";
}
}
3.编写主程序启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 来标注一个启动类 说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
4.运行测试
5.简化部署
<!--可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
将这个应用打包成jar,直接使用java -jar 方式运行 即使没有部署tomcat也可以 因为jar包已经携带了tomcat
6.POM文件
- 1.父项目做依赖管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
- 1.通过artifactId点进去 父项目还依赖一个父项目
管理Spring Boot应用里的所有版本依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
- 2.web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter:场景启动器,当我们要使用web模块的时候,它帮我们导入web模块的相关依赖,当然,依赖的版本都受父项目仲裁
Spring Boot将所有的功能场景抽取出来,做成一个starter,
只需在项目里引入这些starter所有的相关的依赖就会
导入进来,需要什么功能就导入什么场景的starter。
以上就是如何快速搭建一个Spring Boot项目 和相关的pom依赖分析 有没有觉得Boot项上入手就是这么简单。