1. 程式人生 > >SpringBoot入門|-連載1

SpringBoot入門|-連載1

三方 group cto 3.4 spring配置 導入 外部依賴 local .com

1.什麽是Spring Boot?

? 隨著動態語言的流行(Ruby.Grooy.Scala.Nodejs),Java的開發顯得格外的笨重: 繁多的配置、低下的開發效率、復雜的部署流程以及第三方技術集成難度大。
? 在上述環境下,SpringBoot應運而生。它使用“習慣優於配置”(項目中存在大量的配置,此外還內置一個習慣性的配置,讓你無須手動進行配置) 的理念讓你的項目快速運行起來。使用SpringBoot很容易創建一個獨立運行(運行jar,內嵌Servlet容器)準生產級別的基於Sring框架的項目,使用SpringBoot你可以不用或者只需要很少的Spring配置。

2.Spring Boot的優缺點

優點
(1) 快速構建項目;
(2) 對主流開發框架的無配置集成;
(3) 項目可獨立運行,無須外部依賴Servlet容器;
(4) 提供運行時的應用監控;
(5) 極大地提高了開發、部署效率:
(6) 與雲計算的天然集成。

缺點
(1) 書籍文檔較少且不夠深入。
(2) 如果你不認同Spring框架,這也許是它的缺點,但建議你一定要使用Spring框架。

3.創建spring boot 工程

3.1.第一步

技術分享圖片

新建project ,選擇Spring Initializr,選擇jdk,點擊next

3.2.第二步

技術分享圖片

需要修改以下幾個參數

Group:組名

artifact::項目名

java version:jdk版本

packaging:打包方式,如果是web項目打war包,如果不是打成jar包。

點擊next

3.3.第三步

技術分享圖片

根據需要選擇需要的依賴,我們此時暫時不選擇,點擊next。

3.4.第四步

技術分享圖片

設置工程的名稱和路徑,點擊finish完成。

註意:項目新建成功之後maven配置是idea默認的,所以要檢查是否需要修改。

4.pom文件配置及啟動

4.1.設置spring boot的parent

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.10.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

說明:Spring boot的項目必須要將parent設置為spring boot的parent,該parent包含了大量默認的配置,大大簡化了我們的開發。

4.2.導入spring boot的web支持

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

4.3.添加Spring boot的測試插件

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

4.4.添加spring boot的tomcat插件

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

4.5.新建一個StudentController類

@Controller
public class StudentController {
    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "Hello Spring Boot!";
    }
}

4.6.運行spring boot 的入口方法(Application)

運行效果:
技術分享圖片

若看到以上效果則項目啟動成功,此時tomcat默認端口8080。

4.7.測試

打開瀏覽器,輸入localhost:8080/hello,若出現以下效果則成功。

技術分享圖片

SpringBoot入門|-連載1