什么是SpringBoot
Spring Boot 基于 Spring 开发,Spirng Boot 本身并不提供 Spring 框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于 Spring 框架的应用程序。也就是说,它并不是用来替代 Spring 的解决方案,而是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具
Spring Boot 以约定大于配置的核心思想,默认帮我们进行了很多设置,多数 Spring Boot 应用只需要很少的 Spring 配置。同时它集成了大量常用的第三方库配置(例如 Redis、MongoDB、Jpa、RabbitMQ、Quartz 等等),SpringBoot 应用中这些第三方库几乎可以零配置的开箱即用
接下来我们将快速的创建一个Spring Boot应用,并且实现一个简单的Http请求处理
新建项目
新建项目选择spring initalizr,填写项目信息,选择初始化的组件Spring Web,然后等待项目构建成功
项目结构分析
可以看到SpringBoot项目结构下有一个程序的主启动类,一个 application.properties 配置文件,一个 测试类,一个 pom.xml
pom.xml 分析
<!-- 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- web场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
编写HTTP接口
在主程序的同级目录下,新建一个controller包,在包中新建一个HelloController类
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
接下来我们就可以从主程序启动项目,向浏览器发起请求http://localhost:8080/hello,页面返回Hello World,这样我们就完成了一个web接口的开发
项目打包
点击 maven的 package将项目打包,可以看到在target目录下生成一个 jar 包
我们可以在任何地方运行这个jar包,使用java -jar xxx.jar命令
本文介绍了SpringBoot的基本概念,强调其以约定优于配置的理念,并展示了如何使用Spring Initializr新建项目,分析了项目结构,接着详细讲解了如何编写HTTP接口,最后说明了如何打包和运行SpringBoot应用。

1万+

被折叠的 条评论
为什么被折叠?



