Spring Boot 框架快速入門

前言

什么是 Spring Boot ?

Spring Boot簡(jiǎn)化了基于Spring的應(yīng)用開發(fā),你只需要"run"就能創(chuàng)建一個(gè)獨(dú)立的,產(chǎn)品級(jí)別的Spring應(yīng)用。 它的目的是幫助開發(fā)人員很容易的創(chuàng)建出獨(dú)立運(yùn)行和產(chǎn)品級(jí)別的基于 Spring 框架的應(yīng)用。Spring Boot 會(huì)選擇最適合的 Spring 子項(xiàng)目和第三方開源庫(kù)進(jìn)行整合。大部分 Spring Boot 應(yīng)用只需要非常少的配置就可以快速運(yùn)行起來。

創(chuàng)建第一個(gè) Spring Boot 工程

訪問 Spring 生成一個(gè) Spring Boot 的項(xiàng)目。

15027995968925.jpg

填寫 GroupArtifact, 然后點(diǎn) Generate Project 生成你的工程。

使用 IntelliJ 導(dǎo)入工程。

15029600782367.jpg

選擇工程目錄

15029601253270.jpg

然后反復(fù) Next 吧。

工程目錄結(jié)構(gòu)

15029603258858.jpg

最后我們的工程目錄是這樣的。

  • src/main/java 下的 DemoApplication 是程序的入口。
  • src/main/resources 下的 application.properties 是個(gè)配置文件。
  • src/test/java 下的 DemoApplicationTests 是單元測(cè)試的入口。

配置 pom.xml

打開 pom.xml ,可以看到有兩個(gè)默認(rèn)依賴配置

  • spring-boot-starter: 核心模塊,包括自動(dòng)配置支持、日志和YAML
  • spring-boot-starter-test: 測(cè)試模塊, 包括JUnit、Hamcrest、Mockito
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
    </dependency>

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

引入 Web 模塊

要使用 Web 相關(guān)的服務(wù),需要引入 Web 模塊,需添加 spring-boot-starter-web 模塊:

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

寫一個(gè) HelloWorld 服務(wù)

  • 創(chuàng)建 package 命名為 controller (可根據(jù)個(gè)人習(xí)慣修改)
  • 創(chuàng)建 HelloController 類,內(nèi)容如下
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }

}
  • 啟動(dòng)主程序,打開瀏覽器訪問 http://localhost:8080/hello,可以看到頁(yè)面輸出 Hello World

所有的學(xué)習(xí)都是由 Hello World 開始,下次寫寫如何實(shí)現(xiàn)注冊(cè)登錄服務(wù)吧。
后續(xù)所有 Spring Boot 相關(guān)的學(xué)習(xí)源碼,我都會(huì)上傳到這個(gè)倉(cāng)庫(kù)地址上SpringBoot-Learning

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容