本文参考 Spring Boot官方文档 Part II. Getting Started部分
特定版本如1.5.10.RELEASE版本官方文档地址:
https://docs.spring.io/spring...
注:本文基于Spring Boot 1.5.10.RELEASE构建html
The Spring Boot repository has also a bunch of samples you can run.(话说spring-boot在github上已经有23000多颗星了,足见spring-boot火爆程度)java
如下介绍引自 https://projects.spring.io/sp...git
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.github
Featuresweb
Spring Boot 1.5.10.RELEASE版本须要redis
Java 7 and Spring Framework 4.3.14.RELEASE or above 最好java8及以上
构建工具:Maven (3.2+), and Gradle 2 (2.9 or later) and 3.spring
Servlet容器:apache
这里笔者采用构建Empty Project做为Spring Boot学习过程的总工程,以添加Module的方式构建不一样学习过程如springboot-first-app、springboot-redis等浏览器
在Idea欢迎页点击Create New Project或File --> New Project,弹出如下界面并选择Empty Projectspringboot
点击Next,填写Project name如SpringBootLearning
点击Finish完成建立,进入工程Idea会弹出Project Structure窗口,能够点击绿色的+按钮来New Module
也能够经过File --> New --> Module
以上构建空工程的步骤只是笔者为了自身的系统学习,读者能够直接建立一个Spring Boot工程
方法一:在 Spring Boot官方Initializer页面 在线构建工程再导入到Ide中
方法二:直接在Idea中Create New Project --> Spring Initializr --> 填写group、artifact -->钩上web --> 点下一步就好了,具体步骤参照笔者New Module步骤便可
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>cn.fulgens.springbootlearning</groupId> <artifactId>springboot-first-app-maven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-first-app-maven</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package cn.fulgens.springbootlearning.springbootfirstappmaven.web; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") public String hello() { return "Hello Spring Boot!"; } }
方法一:直接运行SpringbootFirstAppMavenApplication中的main方法
package cn.fulgens.springbootlearning.springbootfirstappmaven; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootFirstAppMavenApplication { public static void main(String[] args) { SpringApplication.run(SpringbootFirstAppMavenApplication.class, args); } }
启动后在浏览器输入localhost:8080
方法二:maven命令启动
cd到项目主目录后执行(其中-Dtest.skip=true表示跳过单元测试)
mvn clean package mvn spring-boot:run -Dtest.skip=true
若是是Gradle构建可执行
gradle build
gradle bootRun
方法三:以java -jar的方式启动
maven打包完成后cd到target目录下执行,jar包名称视本身状况而定
java -jar springboot-first-app-maven-0.0.1-SNAPSHOT.jar
package cn.fulgens.springbootlearning.springbootfirstappmaven; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpMethod; 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.result.MockMvcResultMatchers; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void helloControllerTest() throws Exception { mockMvc.perform(MockMvcRequestBuilders.request(HttpMethod.GET, "/")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().string("Hello Spring Boot!")); } }
spring boot 1.4.0 版本以前使用如下三个注解,参考Spring Boot 系列(二)单元测试&网络请求@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = DemoApplication.class) //在spring boot 1.4.0 版本以后取消了 //classes须要指定spring boot 的启动类如:DemoApplication.class 否则WebApplicationContext不被实例化@WebAppConfiguration