spring boot入門 -- 介紹和第一個例子

轉載:https://www.cnblogs.com/junyang/p/8151802.htmlhtml

「愈來愈多的企業選擇使用spring boot 開發系統,spring boot牛在什麼地方?難不難學?心動不如行動,讓咱們一塊兒開始學習吧!」java

使用Spring boot ,能夠輕鬆的建立獨立運行的程序,很是容易構建獨立的服務組件,是實現分佈式架構、微服務架構利器。Spring boot簡化了第三方包的引用,經過提供的starter,簡化了依賴包的配置。git

 

Spring boot的優勢github

  • 輕鬆建立獨立的Spring應用程序。
  • 內嵌Tomcat、jetty等web容器,不須要部署WAR文件。
  • 提供一系列的「starter」 來簡化的Maven配置。
  • 開箱即用,儘量自動配置Spring。

 spring boot 快速入門web

經過構建簡單的REST應用,瞭解spring boot的開發基本流程,驗證其簡單、易用特性。spring

環境要求apache

Spring Boot 2.0.0.BUILD-SNAPSHOT 要求 Java 8 和 Spring Framework 5.0.2以上,Maven 3.2 以上或者Gradle 4。架構

本文使用 Spring Boot 1.5.9 、 Java8 和 Spring Framework 5.0.2.RELEASE以上,Maven 3.2。開發工具使用sping官方提供的spring suit tool 3.9.1(STS)。mvc



建立項目app

在STS中,經過NEW->Spring starter project建立spring boot 項目。

 

 

輸入maven的group 和artifact。

 

 

選擇spring boot版本和starter

 

 點擊下一步,進入以下界面。

選擇spring boot的版本,這裏選擇1.5.9 版本。

選擇starter,經過搜索找到web 並勾選。點擊完成。

 

 

建立項目的結構

點擊finish 按鈕。建立項目以下:

 

目錄結構如圖。

  • Src/main/java。編寫代碼存放的目錄。自動生成了程序入口代碼 SpringBootDemo1Application.java。
  • Src/main/resources。資源文件存放目錄。自動生成了配置文件 application.properties
  • Src/test/java。測試代碼存放目錄。自動生成了測試代碼SpringBootDemo1ApplicationTests.java

POM文件說明

 spring boot項目默認使用maven來構建,生成的POM文件以下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<? xml  version="1.0" encoding="UTF-8"?>
< project  xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     < modelVersion >4.0.0</ modelVersion >
 
     < groupId >com.yuny</ groupId >
     < artifactId >demo1</ artifactId >
     < version >0.0.1-SNAPSHOT</ version >
     < packaging >jar</ packaging >
 
     < name >spring-boot-demo1</ name >
     < description >Demo project for Spring Boot</ description >
 
     < parent >
         < groupId >org.springframework.boot</ groupId >
         < artifactId >spring-boot-starter-parent</ artifactId >
         < version >1.5.9.RELEASE</ version >
         < relativePath />
     </ parent >
 
     < properties >
         < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding >
         < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding >
         < java.version >1.8</ java.version >
     </ properties >
 
     < dependencies >
         < dependency >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-web</ artifactId >
         </ dependency >
 
         < dependency >
             < groupId >org.springframework.boot</ groupId >
             < artifactId >spring-boot-starter-test</ artifactId >
             < scope >test</ scope >
         </ dependency >
     </ dependencies >
 
     < build >
         < plugins >
             < plugin >
                 < groupId >org.springframework.boot</ groupId >
                 < artifactId >spring-boot-maven-plugin</ artifactId >
             </ plugin >
         </ plugins >
     </ build >
</ project >

  

其中,

設置spring-boot-starter-parent爲父親項目

這種方式能夠比較容易的使用父項目中的starters的依賴。 固然也能夠不用繼承spring-boot-starter-parent爲父親,這種方式在之後咱們會介紹。

1
2
3
4
5
6
< parent >
     < groupId >org.springframework.boot</ groupId >
     < artifactId >spring-boot-starter-parent</ artifactId >
     < version >1.5.9.RELEASE</ version >
     < relativePath />
</ parent >

 

引入web依賴

Web starter依賴引入,會增長web容器、springweb、springmvc、jackson-databind等相關的依賴。

1
2
3
4
< dependency >
     < groupId >org.springframework.boot</ groupId >
     < artifactId >spring-boot-starter-web</ artifactId >
</ dependency >

依賴層級關係如圖

 

 

 

引入測試依賴

1
2
3
4
5
< dependency >
     < groupId >org.springframework.boot</ groupId >    
         < artifactId >spring-boot-starter-test</ artifactId >
     < scope >test</ scope >
</ dependency >

 

啓動程序SpringBootDemo1Application 說明  

咱們經過此類的main函數來啓動spring boot程序。

啓動程序SpringBootDemo1Application是自動生成的,代碼以下:

按 Ctrl+C 複製代碼
按 Ctrl+C 複製代碼

 其中是@SpringBootApplication組合註解,兼備了@EnableAutoConfiguration和@ComponentScan 註解的功能。

 

增長一個controller

在包com.yuny.demo1.controller下面增長類SampleController

1
2
3
4
5
6
7
@RestController
public class SampleController {
     @RequestMapping("/")
     String home() {
         return "Hello World!";
     }
}

運行啓動程序後,訪問http://localhost:8080/就能夠訪問這個controller的功能了。

啓動很簡單,直接選擇SpringBootDemo1Application.java文件,使用java application方式運行便可:

 

 

訪問效果:

測試

增長一個測試類

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mport  static  org.hamcrest.CoreMatchers.equalTo;
import  static  org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import  static  org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
@RunWith (SpringJUnit4ClassRunner. class )
@SpringBootTest (classes = MockServletContext. class )
@WebAppConfiguration
public  class  SampleControllerTest {
     private  MockMvc mock;
     
     @Before
     public  void  setUp()  throws  Exception {
         mock = MockMvcBuilders.standaloneSetup( new  SampleController()).build();
     }
 
     @Test
     public  void  testHome()  throws  Exception {
         mock.perform(MockMvcRequestBuilders.get( "/" ).accept(MediaType.APPLICATION_JSON))
             .andExpect(status().isOk())
             .andExpect(content().string(equalTo( "Hello World!" )));
     }
}

 

本文案例代碼地址

https://github.com/junyanghuang/spring-boot-samples/tree/master/springb-01-first

相關文章
相關標籤/搜索