springboot系列01-入门篇
Spring Boot是什么
Spring Boot 是由 Pivotal 团队提供的全新框架,目的是用来简化新 Spring 应用的初始搭建以及开发过程。框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。就是 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 Maven 整合了所有的 Jar 包,Spring Boot 整合了所有的框架。
使用Spring Boot有什么好处
简单、快速、方便!
对比,搭建一个 Spring Web 项目的时候需要怎么做
-
1)配置 web.xml,加载 Spring 和 Spring mvc
-
2)配置数据库连接、配置 Spring 事务
-
3)配置加载配置文件的读取,开启注解
-
4)配置日志文件
-
…
-
配置完成之后部署 servlet容器
-
…
使用springboot,只需要非常少的几个配置就可以迅速方便的搭建起来一套 Web 项目
快速入门
-
1、访问 http://start.spring.io
-
2、选择构建工具 Maven Project、Java、Spring Boot 版本
-
3、点击 Generate 下载项目压缩包
-
4、解压后,使用 Idea 导入项目,File -> New -> Model from Existing Source.. -> 选择解压后的文件夹 -> OK,选择 Maven -> 完成
-
5、如果使用的是 Eclipse,Import -> Existing Maven Projects -> Next -> 选择解压后的文件夹 -> 完成
Spring Boot 的基础结构共三个文件:
-
src/main/java
程序开发以及主程序入口 -
src/main/resources
配置文件 -
src/test/java
测试程序
Spring Boot 启动
1、pom.xml模块
1 2 3 4 5 6 | <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.1 . 7 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> |
添加支持web的模块:
1 2 3 4 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
添加支持test的模块:
1 2 3 4 5 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> |
pom.xml 文件中默认有两个模块:
-
spring-boot-starter
:核心模块,包括自动配置支持、日志和 YAML,如果引入了spring-boot-starter-web
web 模块可以去掉此配置,因为spring-boot-starter-web
自动依赖了spring-boot-starter
。 -
spring-boot-starter-test
:测试模块,包括 JUnit、Hamcrest、Mockito
2、编写 Controller 内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping ( "/hello" ) public String index() { return "Hello World" ; } } |
@RestController
就是 Controller 里面的方法都以 json 格式输出,不用再写什么 jackjson 配置
3、启动主程序DemoApplication,打开浏览器访问 http://localhost:8080/hello
看到效果
4、另编写单元测试
在src/test/下编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.example.demo; import com.example.demo.controller.HelloWorldController; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.hamcrest.core.IsEqual.equalTo; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith (SpringRunner. class ) @SpringBootTest public class HelloTest { private MockMvc mvc; @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup( new HelloWorldController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get( "/hello" ).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo( "Hello World" ))); } } |
开发环境调试
springBoot对调试支持很好,修改之后可以实时生效,需要添加以下的配置pom.xml:
1 2 3 4 5 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional> true </optional> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 | <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork> true </fork> <!-- 重要 --> </configuration> </plugin> </plugins> </build> |
该模块在完整的打包环境下运行的时候会被禁用!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!