spring-boot快速入門實踐代碼

spring-boot我想已經有很多人瞭解太的概念了吧,不瞭解的你可以網上一查一大堆,但是概念有的時候還是很難讓人深入理解,我知道它,但是它是幹什麼的就不知道了。

以下摘要是網上對比spring的,看客可以參考以下:

[plain] view plain copy
  1. Springboot之Spring與Spring boot的區別:一直以來,由於對SPRING的理解不深入。在使用中總是Spring boot的時候認爲它是SPRING的一部分,事實不然。  
  2.   
  3. Spring boot是一個在Spring 的基礎上搭建的全新的微框架,其目的是簡化Spring的搭建和開發過程。  
  4.   
  5. 有以下的特點:  
  6. 1)簡單易用,初學者和大牛都可以輕鬆上手,其中的註解會給使用者提供方便;  
  7.   
  8. 2)Spring boot對第三方技術進行了很好的封裝和整合,提供了大量第三方接口;  
  9.   
  10. 3)可以通過依賴自動配置,不需要XML等配置文件  
  11.   
  12. 4)還提供了安全等特性姑且先不做理會。  

 

我們今天主要是快速啓動一個spring-boot的項目,然後每天往裏面加點東西,一點一點的加進去,項目再不斷的完善,即可慢慢了解它到底有什麼好處,能給我們帶來哪些好處和便捷。

今天首先第一步,從頭搭建項目,你也可以直接從官網上下載一個,也可以通過maven自己創建,我這裏是通過我本地自己創建maven的




填寫好相關的信息,然後點擊finish完成即可

項目創建完之後,就需要往裏面添加spring-boot所需要的jar包了,首先找到項目的pom.xml文件,然後在裏面添加如下代碼:

[html] view plain copy
  1. <parent>  
  2.       <groupId>org.springframework.boot</groupId>  
  3.       <artifactId>spring-boot-starter-parent</artifactId>  
  4.       <version>1.5.7.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>  

添加完之後,接下來就是要設置啓動類了,在項目的src目錄下,創建一個com.lwl.boot.controller的包,然後創建MyApplication.java類:具體如下

[java] view plain copy
  1. package com.lwl.boot.controller;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  5. import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;  
  6. import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;  
  7. import org.springframework.boot.web.servlet.ErrorPage;  
  8. import org.springframework.context.annotation.Bean;  
  9. import org.springframework.http.HttpStatus;  
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.ResponseBody;  
  13.   
  14. @Controller  
  15. @EnableAutoConfiguration  
  16. /** 
  17.  * @EnableAutoConfiguration 用於自動配置。簡單的說,它會根據你的pom配置(實際上應該是根據具體的依賴)來判斷這是一個什麼應用,並創建相應的環境。 
  18.  */  
  19. public class MyApplication {  
  20.   
  21.   
  22.     @RequestMapping("/")  
  23.     @ResponseBody  
  24.     public String index() {  
  25.         return "你現在看到的是帥帥的我";  
  26.     }  
  27.       
  28.       
  29.     @RequestMapping("/hello")  
  30.     @ResponseBody  
  31.     public String hello() {  
  32.         return "hello 帥帥的我出現啦";  
  33.     }  
  34.   
  35.       
  36.       
  37.     public static void main(String[] args) throws Exception {  
  38.         /** 
  39.          * SpringApplication 則是用於從main方法啓動Spring應用的類。默認,它會執行以下步驟: 
  40.             1.創建一個合適的ApplicationContext實例 (取決於classpath)。 
  41.             2.註冊一個CommandLinePropertySource,以便將命令行參數作爲Spring properties。 
  42.             3.刷新application context,加載所有單例beans。 
  43.             4.激活所有CommandLineRunner beans。 
  44.             默認,直接使用SpringApplication 的靜態方法run()即可。但也可以創建實例,並自行配置需要的設置。 
  45.          */  
  46.         SpringApplication.run(MyApplication.class, args);  
  47.           
  48.         //通過自定義參數設置,啓動  
  49. //        SpringApplication app = new SpringApplication(MyApplication.class);  
  50. //        app.run(args);  
  51.           
  52.           
  53.         /** 
  54.          * 如果設置 properties 則配置參數會讀取properties設置的值 
  55.          */  
  56.         
  57.           
  58.     }  
  59.       
  60.     /** 
  61.      * 設置基本異常處理拋出頁面 
  62.      * @return 
  63.      * @create 2017-10-9 上午11:44:13 
  64.      */  
  65.     @Bean  
  66.     public EmbeddedServletContainerCustomizer containerCustomizer() {  
  67.         return new EmbeddedServletContainerCustomizer() {  
  68.             @Override  
  69.             public void customize(ConfigurableEmbeddedServletContainer container) {  
  70.                 ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");  
  71.                 ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");  
  72.                 ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");  
  73.                 container.addErrorPages(error401Page, error404Page, error500Page);                
  74.             }  
  75.         };  
  76.     }  
  77.       
  78. }  

右擊→啓動這個類即可加載項目

[html] view plain copy
  1. 如果設置 properties 則配置參數會讀取properties設置的值  

這個有個特別的配置文件,以後會慢慢詳解,先來看看項目運行起來的效果


打開瀏覽器,查看運行效果http://localhost:8090/hello



代碼已經上傳到我的github上面,如果有需要可以去下載:https://github.com/1181888200/spring-boot.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章