简介
Spring Boot可以轻松创建可以“运行”的独立的,生产级的基于Spring的应用程序。大多数Spring Boot应用程序需要很少的Spring配置。
特点:
- 创建独立的Spring应用程序
- 直接嵌入Tomcat,Jetty或Undertow(不需要部署WAR文件)
- 提供有封装的“starts”POM来简化Maven配置
- 尽可能自动配置
- 提供生产检测功能,如指标,运行状况检查和外部化配置
- 不需要代码生成,也不需要XML配置
快速入门
环境配置:
jdk版本:
java version “1.8.0_111”
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
gradle版本:Gradle 3.2.1
项目结构:
gradle配置:
group 'cn.he'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
//引入web启动项
compile 'org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
一切准备就绪编写rest接口
package cn.he;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
启动运行,
浏览器输入http://localhost:8080/ 即可访问
程序员见的最多的两个单词
到此第一个springboot项目算是基本完成。大家也可以在通过SPRING INITIALIZR工具产生基础项目,这里就不做介绍。有兴趣的同学可以自己尝试一下。
通过第一个项目发现,springboot在没有配置的情况下就可以搭建出一个web项目,也充分说明了它习惯大于配置的这一属性。