Spring Boot
Spring Boot
Spring Boot
2017/4/22
基本概念
基本概念
由於Spring Framework的設定相當複雜,所以,Spring也推出Spring Boot,讓設定變得簡單了,而且可以產生獨立的Spring App,Spring Boot也內建Tomcat及Jetty,讓開發MVC變得相對簡單多了。目前Spring Boot的版本是1.5.3 (2017/4/21 released),內建Tomcat 7 & 8、Jetty 8, 9.2 & 9.3、Undertow 1.3,需要Java 7以上(建議 Java 8)及Spring Framework 4.3.7以上。
新增一個Spring Boot MVC專案
新增一個Spring Boot MVC專案
在使用Eclipse的請況下:
- 利用Spring STS產生
- File/New/Other…
- Spring/Spring Starter Project
- 設定基本參數 (如:專案名稱,packaging的方式是JAR)
- 選擇所需要的Dependencies (如:Web & Thymeleaf)
- 匯入壓縮過的Spring project
- File/Import
- General/Existing Projects into workspace
- Select archive file
- 匯入後,可能會因為你的環境與原專案不同,必須要
- 更新專案 (選專案,按右鍵,選Maven,Update Project,記得勾選 Force Update of Snapshots/releases。)。Maven會協助我們下載應有的package,為了降低更新頻率,不會自動檢查並下載新版本。當在學校或在家裡使用的版本不一致時,就必須利用更新專案來強迫更新版本。
- 調整build path
- 調整server
Spring Boot會產生一個java檔,如:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
當我們加了@SpringBootApplication,Spring會自動scan這個這個package以下的目錄,所以,其他的檔案要放在com.example.demo下,如: com.example.demo.controller。那如果其他的檔案是放在com.example.controller,就會找不到。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
參考資料
參考資料
- http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/
- https://projects.spring.io/spring-boot/
- https://spring.io/guides/gs/serving-web-content/
- https://spring.io/understanding/view-templates
- https://spring.io/guides/gs/serving-web-content/
- https://spring.io/guides/gs/handling-form-submission/
- https://spring.io/guides/gs/validating-form-input/
- Spring Boot 介紹 (中文、Spring Boot 1.3.3)
- http://ithelp.ithome.com.tw/articles/10161665 (中文、Spring Boot 1.1.8)
- http://peaceful-developer.blogspot.tw/2014/08/spring-1-spring-boot.html (中文、Spring Boot 1.1.5)
![](https://www.google.com/images/icons/product/drive-32.png)