Spring Boot 入門篇
Spring Boot 相比Spring Framework框架的繁雜配置(各種XML配置與代碼CP)更輕量化,內嵌Web 容器(默認Tomcat)啟動應用隻需一個類即可;使Spring開發者能更快的入門,大大的提高了開發效率, 下文開始演示Spring Boot的入門(英文與技術基礎好的同學,可以直進入官網看demo學習)。
開發環境說明:
- java version "1.8.0_91"
- Spring Boot 1.5.2.RELEASE 用例親自調試通過,放心入坑。
- Maven構建POM
#代碼引用
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <!-- 引入Web模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
- Gradle構建POM
#代碼引用
dependencies { compile("org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE") }
#項目目錄結構
新建類
hello/SampleController.java
代碼引用
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
啟動應用主程序SampleController
啟動日誌
- 啟動默認用了Tomcat 端口8080
- Spring Boot 版本號1.5.2
- 無需任何配置文件申明
- 下麵開始使用Postman來請求應用,結果如圖
- 也可以直接在瀏覽器上麵輸入:
https://localhost:8080/
'Hello World!'
本項目演示了,通過Maven 構建項目,引用Spring Boot中
spring-boot-starter-parent
spring-boot-starter-web
實現了基礎的Web應用,處理簡單的請求,下篇開始引用application.properties 或 application.yml 實現修改應用默認端口與數據庫訪問。
參考資料:
https://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/
最後更新:2017-04-01 16:41:01