springboot【1】入门之初识
来源:互联网 发布:mac系统内存占用高 编辑:程序博客网 时间:2024/05/29 15:13
简介
Spring Boot是Spring团队推出的新框架,它所使用的核心技术还是Spring框架,主要是Spring 4.x,所以如果熟悉spring 4的人,能够更快的接受和学会这个框架。Spring boot可以看做是在spring框架基础上再包了一层,这一层包含方便开发者进行配置管理和快速开发的模块,以及提供了一些开箱即用的工具,比如监控等。
Spring Boot让我们的Spring应用变的更轻量化。比如:你可以仅仅依靠一个Java类来运行一个Spring引用。你也可以打包你的应用为jar并通过使用java -jar来运行你的Spring Web应用。
Spring Boot的主要优点:
- 为所有Spring开发者更快的入门
- 开箱即用,提供各种默认配置来简化项目配置
- 内嵌式容器简化Web项目
- 没有冗余代码生成和XML配置的要求
快速入门
主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。
系统要求:
- Java 7及以上
- Spring Framework 4.1.5及以上
本文采用Java 1.7.0_72
、Spring Boot 1.3.2
、maven构建项目 调试通过。
项目结构解析
为了规范合理,建立一个父工程,接下来将通过增加子模块的方式,来系统学习。
通过上面步骤完成了基础项目的创建,如上图所示,Spring Boot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group所有差异):
src/main/java
下的程序入口:Main
src/main/resources
下的配置文件:application.properties
src/test/
下的测试入口:HelloWorldTest
生成的
和Main
HelloWorldTest
类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。
引入所需的包
当前的pom.xml
内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.lyd</groupId><artifactId>springboot</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>example01</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${springboot.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><version>${springboot.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>${springboot.version}</version></dependency></dependencies><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.7</java.version><springboot.version>1.3.2.RELEASE</springboot.version></properties></project>
编写HelloWorld服务
- 创建
package
命名为com.lyd.web
(根据实际情况修改) - 创建
HelloController
类,内容如下:
package com.lyd.web;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {@RequestMapping("/hello")public String index(){return "Hello World";}}
启动主程序,打开浏览器访问
http://localhost:8080/hello
,可以看到页面输出Hello World
主程序Main代码:
package com.lyd;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Main {public static void main(String[] args) {SpringApplication.run(Main.class, args);}}
编写单元测试用例
打开的src/test/
下的测试入口HelloWorldTest
类。下面编写一个简单的单元测试来模拟http请求,具体如下:
package com.lyd;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.mock.web.MockServletContext;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.http.MediaType;import com.lyd.web.HelloController;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;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = MockServletContext.class)@WebAppConfigurationpublic class HelloWorldTest {private MockMvc mvc;@Beforepublic void setUp(){mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();}@Testpublic void getHello() throws Exception{mvc.perform(MockMvcRequestBuilders.get("/hello").accept(org.springframework.http.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模块实现了一个简单的请求处理。
下面是完整项目结构:
源码下载:example01
- springboot【1】入门之初识
- SpringBoot入门(七)-初识WebSocket
- 1、初识SpringBoot
- 【SpringBoot】SpringBoot之入门配置文件
- SpringBoot初识和快速入门(一)
- SpringBoot学习1之快速入门
- SpringBoot入门之HelloWorld
- SpringBoot入门之HelloWorld
- SpringBoot入门之数据库
- springboot入门之mybatis
- SpringBoot实战之入门
- 初识SpringBoot,Mavern之环境搭建
- 初识JRuby之入门
- 初识JRuby之入门
- SpringBoot应用与原理之SpringBoot入门
- SpringBoot入门之新建一个SpringBoot项目
- SpringBoot入门之如何启动SpringBoot
- 初识Springboot
- 关于python ide
- 转载-android studio 各种问题
- [置顶]总结汇总
- PLC单按钮起停例程
- 紧绷安全之弦,铸就信息安全穹顶——祥鹏IT抗击“永恒之蓝”勒索病毒纪实
- springboot【1】入门之初识
- app测试1--fiddler4工具的使用
- Windows下搭建PHP开发环境
- 【笔记】【imooc】canvas(绚丽的时钟效果)
- codeforces822B
- usb audio--异步传输方式描述符说明(2)
- 数据库之MySQL新手上路
- java 把请求数据转换成map数据
- Hibernate——1+N问题