Spring Boot是Spring团队设计用来简化Spring应用的搭建和开发过程的框架。该框架对第三方库进行了简单的默认配置,经过Spring Boot构建的应用程序只需不多的Spring配置便可快速的运行起来。java
简单、快速、便捷。git
搭建普通Spring Web项目项目的通常流程:github
.......web
一系列的配置过程繁杂,很容易遗漏掉,即使是随便搭建一个简单的爬取某个页面的邮箱存入数据库的小项目,都要从头至尾的将配置流程走一遍。spring
若是使用Spring Boot,配置则简化了不少,只须要引入响应的几个maven依赖,进行简单的几个配置就能够快速方便的搭建一个Web项目。数据库
备注:json
Dependencies处可经过添加相应的依赖,在生成项目文件时就会自动添加,好比,若是咱们要作Web开发,只要在依赖里添加Web便可。浏览器
Tips:tomcat
导入或者Myeclipse构建项目有可能会出现pom.xml文件首行出现错误:Unknown pom.xml /demo line 1 Maven Configuration Problem。微信
解决方案:
在pom文件中的<properties>节点中加入<maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>而后右键项目进入Maven>Update Project...菜单点击,便可
Spring Boot 的基础结构共三个文件:
src/main/java
程序开发以及主程序入口:Applicationsrc/main/resources
配置文件存放位置:application.propertiessrc/test/java
测试入口:ApplicationTests生成的Application
和ApplicationTests
类均可以直接运行来启动当前建立的项目,因为目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring以后结束运行。
spring-boot-starter-parent指定了当前项目为一个Spring Boot项目,它提供了诸多的默认Maven依赖。
Spring Boot提供了许多开箱即用的依赖模块,这些模块都是以spring-boot-starter-XX命名的。好比要开启Spring Boot的web功能,只须要在pom.xml中配置spring-boot-starter-web便可:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
由于其依赖于spring-boot-starter-parent,因此这里能够不用配置version。
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。package com.w3cjava.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
至关于Spring中的@Controller和@ResponseBody组合使用的,直接以 json 格式输出。
<!-- 支持web的模块依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除tomcat依赖 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
<!-- jetty依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
模拟对 http://localhost:8080/hello 发送请求测试
package com.w3cjava; import static org.hamcrest.CoreMatchers.equalTo; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 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 com.w3cjava.controller.HelloWorldController; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { 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"))); } }
<!-- 热部署依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
@RequestMapping("/hello1") public String index1() { return "Hello World1"; }
spring-boot-maven-plugin:可以以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Maven操做的可能。
Spring Boot Maven plugin的5个Goals
整体上而言,经过Spring Boot能够快速构建项目,若是须要使用某个特定的功能,只要添加对应的依赖及简单配置项便可。
欢迎扫面下列二维码关注“余弦的自留地”公众微信号万物之中,但愿至美