简介
在您第1次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候,是否觉得一堆反复黏贴的配置有一些厌烦?那么您就不妨来试试使用Spring Boot来让你更易上手,更简单快捷地构建Spring应用!
Spring Boot让我们的Spring应用变的更轻量化。比如:你可以仅仅依靠一个Java类来运行一个Spring引用。你也可以打包你的应用为jar并通过使用java -jar来运行你的Spring Web应用。
Spring Boot的主要优点:
- 为所有Spring开发者更快的入门
- 开箱即用,提供各种默认配置来简化项目配置
- 内嵌式容器简化Web项目
- 没有冗余代码生成和XML配置的要求
https://blog.didispace.com/spring-boot-learning-1/#%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8 快速入门
本章主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。
https://blog.didispace.com/spring-boot-learning-1/#%E7%B3%BB%E7%BB%9F%E8%A6%81%E6%B1%82%EF%BC%9A 系统要求:
- Java 7及以上
- Spring Framework 4.1.5及以上
本文采用
Java 1.8.0_73
、
Spring Boot 1.3.2
调试通过。
https://blog.didispace.com/spring-boot-learning-1/#%E4%BD%BF%E7%94%A8Maven%E6%9E%84%E5%BB%BA%E9%A1%B9%E7%9B%AE 使用Maven构建项目
- 通过
工具产生基础项目SPRING INITIALIZR
Spring Boot快速入门简介快速入门
-
- 点击
下载项目压缩包Generate Project
- 点击
- 解压项目包,并用IDE以
项目导入,以Maven
为例:IntelliJ IDEA 14
-
- 菜单中选择
–>File
New
Project from Existing Sources...
- 选择解压后的项目文件夹,点击
OK
-
并选择Import project from external model
,点击Maven
到底为止。Next
- 若你的环境有多个版本的JDK,注意到选择
的时候请选择Java SDK
以上的版本Java 7
- 菜单中选择
项目结构解析
通过上面步骤完成了基础项目的创建,如上图所示,Spring Boot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group所有差异):
-
下的程序入口:src/main/java
Chapter1Application
-
下的配置文件:src/main/resources
application.properties
-
下的测试入口:src/test/
Chapter1ApplicationTests
生成的
Chapter1Application
和
Chapter1ApplicationTests
类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。
https://blog.didispace.com/spring-boot-learning-1/#%E5%BC%95%E5%85%A5Web%E6%A8%A1%E5%9D%97 引入Web模块
当前的
pom.xml
内容如下,仅引入了两个模块:
-
:核心模块,包括自动配置支持、日志和YAMLspring-boot-starter
-
:测试模块,包括JUnit、Hamcrest、Mockitospring-boot-starter-test
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
引入Web模块,需添加
spring-boot-starter-web
模块:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
编写HelloWorld服务
- 创建
命名为package
(根据实际情况修改)com.didispace.web
-
类,内容如下HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}
- 启动主程序,打开浏览器访问
,可以看到页面输出http://localhost:8080/hello
Hello World
https://blog.didispace.com/spring-boot-learning-1/#%E7%BC%96%E5%86%99%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B 编写单元测试用例
打开的
src/test/
下的测试入口
Chapter1ApplicationTests
类。下面编写一个简单的单元测试来模拟http请求,具体如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class Chapter1ApplicationTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).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")));
}
}
使用
MockServletContext
来构建一个空的
WebApplicationContext
,这样我们创建的
HelloController
就可以在
@Before
函数中创建并传递到
MockMvcBuilders.standaloneSetup()
函数中。
- 注意引入下面内容,让
status
content
函数可用equalTo
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
至此已完成目标,通过Maven构建了一个空白Spring Boot项目,再通过引入web模块实现了一个简单的请求处理。
https://blog.didispace.com/spring-boot-learning-1/#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B 代码示例
本文的相关例子可以查看下面仓库中的
chapter1
目录:
- Github: https://github.com/dyc87112/SpringBoot-Learning
- Gitee: https://gitee.com/didispace/SpringBoot-Learning
如果您觉得本文不错,欢迎
Star
支持,您的关注是我坚持的动力!
推荐: 使用Cloud Studio在线编写、调试和管理Spring Boot应用