spring boot:閃電上手

          spring-boot是什么?
          spring-boot是spring的一個子項目,spring-boot旨在能夠快速構建基于spring的獨立的,產品級別的應用,擁有“一鍵啟動”,“配置簡約”,“內置服務器”,“應用健康檢查”等一系列高逼格的功能。

          spring-boot hello world
          注:這里的hello world并沒有一板一眼的將官網的案例照搬,而是希望以一種最快速的方式構建一個由spring boot構建的應用,讓包括我在內的對spring boot陌生的同學對它有一個最直觀的感受。

          java


          IDE


          Maven(Eclipse內置)

          你也可以自行配置一個外部Maven,隨意

          接下來我們新建一個Maven項目,我們選取webapp模板



          搭建完畢之后,我們需要修改pom.xml文件,將spring boot相關的依賴添加進去
          首先,添加parent節點到pom.xml
          1     <parent>
          2         <groupId>org.springframework.boot</groupId>
          3         <artifactId>spring-boot-starter-parent</artifactId>
          4         <version>1.3.5.RELEASE</version>
          5     </parent>
          spring boot提供了一系列的“starter POMs”,可以輕松的添加相關的jar到項目的類路徑下,而spring-boot-starter-parent節點則是一個特殊的“starter”,它提供了很多maven默認的依賴,并且提供了依賴管理,使得我們可以忽略依賴的版本號,也就是說我們無需再聲明version標識。

          *starter POMs:可以理解一系列的依賴,每一個依賴都提供了該功能所需的jar包


          我們添加spring-boot-starter-web到pom.xml
           1     <parent>
           2         <groupId>org.springframework.boot</groupId>
           3         <artifactId>spring-boot-starter-parent</artifactId>
           4         <version>1.3.5.RELEASE</version>
           5     </parent>
           6     
           7     <dependencies>
           8         <dependency>
           9             <groupId>org.springframework.boot</groupId>
          10             <artifactId>spring-boot-starter-web</artifactId>
          11         </dependency>
          12     </dependencies>
          之后我們看到構建web應用所需的jar包已經添加到Maven依賴中了


          更多關于“starter”的信息可以查看
          http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-starter

          接下來可以寫代碼了,添加一個“控制器”到src/main/java/demo下
           1 package demo;
           2 
           3 import org.springframework.boot.*;
           4 import org.springframework.boot.autoconfigure.*;
           5 import org.springframework.web.bind.annotation.*;
           6 
           7 @RestController
           8 @EnableAutoConfiguration
           9 public class Example {
          10 
          11     @RequestMapping("/")
          12     String home() {
          13         return "Hello World!";
          14     }
          15 
          16     public static void main(String[] args) throws Exception {
          17         SpringApplication.run(Example.class, args);
          18     }
          19 
          20 }
          之后我們“run as Java application”或者通過Maven goals“spring-boot:run”,就可以發現,我們的服務已經在本地的8080端口啟動了。

          是不是太快了...我也這么覺得,真的是“傻瓜式”的部署

          @RestController
          這個注解是“stereotype annotation”(不知道怎么翻譯合適,或許可以理解為“套版注解”),在閱讀代碼的時候給讀者提供必要的提示,對于spring來說,他扮演了一個特別的角色,在案例中他是一個web的控制器,所以spring會用他來處理對應的請求。

          @RequestMapping
          用過spring-MVC的同學都知道,這個注解用來對應路由

          @EnableAutoConfiguration
          這個注解就比較牛皮了,也是目前為止個人感覺spring boot強大的地方之一。這個注解告知spring,去通過你添加的依賴,“猜測”你想要怎么去配置spring,比如我們添加了start POM spring-boot-starter-web,那么spring會認為我們需要部署一個web應用,所以spring boot幫助我們啟動tomcat服務,配置springMVC等操作。官方文檔同時提到,@EnableAutoConfiguration注解在設計時就與start POMs協作良好,但是二者并不絕對相關,就算沒有start POM,spring boot依然會盡最大努力去自動配置(笑)

          截止目前,排除細節的情況下,已經通過最簡單粗暴的方式構造了一個spring boot的應用,希望在認知上能給大家一點幫助,但是還遠遠不夠,后續還要添加更多與web應用相關的內容。

          posted on 2016-06-01 12:00 都較瘦 閱讀(287) 評論(0)  編輯  收藏 所屬分類: spring boot相關案例積累


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2016年6月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鹤壁市| 泰宁县| 砀山县| 巴彦淖尔市| 高雄县| 手机| 敦化市| 温泉县| 兴和县| 延安市| 庆安县| 伊川县| 新化县| 尉犁县| 建宁县| 潍坊市| 益阳市| 治县。| 宜昌市| 马鞍山市| 宜兰市| 德清县| 通化县| 阳曲县| 贵南县| 永德县| 建湖县| 塔城市| 崇阳县| 平定县| 绵竹市| 淄博市| 同德县| 黄浦区| 景洪市| 宽甸| 宁安市| 济南市| 德阳市| 垫江县| 伊通|