采用固定的形式構(gòu)建準(zhǔn)產(chǎn)品的Spring應(yīng)用程序公浪。Spring Boot支持約定優(yōu)于配置,旨在讓你盡快地啟動并運行Spring應(yīng)用程序鼎姐。
SpringBoot很容易創(chuàng)建基于Spring的獨立的绊困、產(chǎn)品級的,可以“即時運行”的應(yīng)用程序瑰枫。我們對Spring平臺和第三方庫采用固定的形式,這樣你就可以以最小的配置開始使用丹莲。大多數(shù)SpringBoot應(yīng)用程序只需要很少的Spring配置光坝。
產(chǎn)品特點
- 創(chuàng)建可以獨立運行的Spring 應(yīng)用。
- 直接嵌入 Tomcat 或 Jetty 服務(wù)器甥材,不需要部署 WAR 文件盯另。
- 提供固定的'starter' POMs來簡化你的Maven配置。
- 盡可能自動配置Spring洲赵。
- 提供準(zhǔn)產(chǎn)品特性鸳惯,如性能指標(biāo)、健康檢查和外部化配置叠萍。
- 沒有代碼生成芝发,也沒有 XML 配置要求。
- 參考指南包括對所有特性的詳細(xì)描述俭令,以及對常用用例的詳細(xì)說明。
Quick Start
以eclipse為例部宿,需要安裝STS來支持Spring Boot的開發(fā)抄腔,安裝gradle或maven來管理Spring Boot程序的依賴項和構(gòu)建它們。
可以從https://start.spring.io/ 這個地址可以下載一個空的Spring Boot項目理张。以gradle為例赫蛇,導(dǎo)入到STS之后的項目結(jié)構(gòu)如下:
在項目中打開build.gradle文件。
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
可將mavenCentral()替換為maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'} 雾叭,這樣的話就可以用阿里云的maven倉庫了悟耘。下載第三方依賴的時候會很快。
在dependencies下添加compile("org.springframework.boot:spring-boot-starter-web:2.0.0.M2")就可以開發(fā)spring mvc程序了织狐。
更改如果的build.gradle文件:
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
只需在項目中添加SampleController 類暂幼。
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@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);
}
}
然后Run As -> Spring Boot App就可以運動了。打開localhost:8080/可以查看運行效果移迫。