前言
“越来越多的企业选择使用spring boot 开发系统,spring boot牛在什么地方?难不难学?心动不如行动,让我们一起开始学习吧!”
使用Spring boot ,可以轻松的创建独立运行的程序,非常容易构建独立的服务组件,是实现分布式架构、微服务架构利器。Spring boot简化了第三方包的引用,通过提供的starter,简化了依赖包的配置。
Spring boot的优点
- 轻松创建独立的Spring应用程序。内嵌Tomcat、jetty等web容器,不需要部署WAR文件。提供一系列的“starter” 来简化的Maven配置。开箱即用,尽可能自动配置Spring。
spring boot 快速入门
通过构建简单的REST应用,了解spring boot的开发基本流程,验证其简单、易用特性。
环境要求
Spring Boot 2.0.0.BUILD-SNAPSHOT 要求 Java 8 和 Spring Framework 5.0.2以上,Maven 3.2 以上或者Gradle 4。
本文使用 Spring Boot 1.5.9 、 Java8 和 Spring Framework 5.0.2.RELEASE以上,Maven 3.2。开发工具使用sping官方提供的spring suit tool 3.9.1(STS)。
创建项目
在STS中,通过NEW->Spring starter project创建spring boot 项目。

输入maven的group 和artifact。

选择spring boot版本和starter

点击下一步,进入如下界面。
选择spring boot的版本,这里选择1.5.9 版本。
选择starter,通过搜索找到web 并勾选。点击完成。

创建项目的结构
点击finish 按钮。创建项目如下:

目录结构如图。
- Src/main/java。编写代码存放的目录。自动生成了程序入口代码 SpringBootDemo1Application.java。Src/main/resources。资源文件存放目录。自动生成了配置文件 application.propertiesSrc/test/java。测试代码存放目录。自动生成了测试代码SpringBootDemo1ApplicationTests.java
POM文件说明
spring boot项目默认使用maven来构建,生成的POM文件如下:
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.yuny demo1 0.0.1-SNAPSHOTjarspring-boot-demo1Demo project for Spring Bootorg.springframework.boot spring-boot-starter-parent 1.5.9.RELEASEUTF-8UTF-81.8org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test testorg.springframework.boot spring-boot-maven-plugin
其中,
设置spring-boot-starter-parent为父亲项目
这种方式可以比较容易的使用父项目中的starters的依赖。 当然也可以不用继承spring-boot-starter-parent为父亲,这种方式在以后我们会介绍。
org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE
引入web依赖
Web starter依赖引入,会增加web容器、springweb、springmvc、jackson-databind等相关的依赖。
org.springframework.boot spring-boot-starter-web
依赖层级关系如图

引入测试依赖
org.springframework.boot spring-boot-starter-test test
启动程序SpringBootDemo1Application 说明
我们通过此类的main函数来启动spring boot程序。
启动程序SpringBootDemo1Application是自动生成的,代码如下:
@SpringBootApplicationpublic class SpringBootDemo1Application { public static void main(String[] args) { SpringApplication.run(SpringBootDemo1Application.class, args); }}
其中是@SpringBootApplication组合注解,兼备了@EnableAutoConfiguration和@ComponentScan 注解的功能。
增加一个controller
在包com.yuny.demo1.controller下面增加类SampleController
@RestControllerpublic class SampleController { @RequestMapping("/") String home() { return "Hello World!"; }}
运行启动程序后,访问http://localhost:8080/就可以访问这个controller的功能了。
启动很简单,直接选择SpringBootDemo1Application.java文件,使用java application方式运行即可:

访问效果:

测试
增加一个测试类
mport 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; @RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = MockServletContext.class)@WebAppConfigurationpublic class SampleControllerTest { private MockMvc mock; @Before public void setUp() throws Exception { mock = MockMvcBuilders.standaloneSetup(new SampleController()).build(); } @Test public void testHome() throws Exception { mock.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Hello World!"))); }}
我每天都会写Java技术文章,对Java有兴趣的朋友可以关注我,同时需要Java技术资料的也可以私信我,免费送

来源:公众号
作者:动力节点java资源库