01 SpringBoot介紹

相關文檔 http://felord.cn/_doc/_springboot/2.1.5.RELEASE/_book/

1、SpringBoot介紹

SpringBoot是Spring項目中的一個子工程,與我們所熟知的Spring-framework 同屬於spring的產品:

2、SpringBoot好處

在學習SSM過程中,爲了完成框架對接,要進行大量的配置,導入很多的包,如果包之間版本出現不兼容,包之間的依賴管理很麻煩,配置過程佔用了過度的時間。

SpringBoot的設計就是爲了解決這個問題的。

SpringBoot是搭建程序的腳手架,能夠幫助我們導入很多的相關包。要使用相關組件只需要導入對應的starter腳手架即可。很多以前在xml配置內容通過寫代碼的方式實現。

3、SpringBoot特點

  • 爲所有 Spring 的開發者提供一個非常快速的、廣泛接受的入門體驗
  • 開箱即用(啓動器starter-其實就是SpringBoot提供的一個jar包),但通過自己設置參數(.properties),即可快速擺脫這種方式。
  • 提供了一些大型項目中常見的非功能性特性,如內嵌服務器、安全、指標,健康檢測、外部化配置等
  • 絕對沒有代碼生成,也無需 XML 配置。

4、SpringBoot搭建Web項目體驗

1)在磁盤上創建SpingBoot目錄

2)通過Idea打開該目錄

3)創建Maben類型的模塊

4)pom.xml中配置JDK版本

    <!--設置JDK版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

5)pom.xml中添加父工程座標

    <!--
    添加SpringBoot的基礎工程爲父工程,這個項目會把包之間的依賴關係處理好,不需要我們處理了
    -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
    </parent>

SpringBoot提供了一個名爲spring-boot-starter-parent的工程,內部包含了常用的依賴包(並非全部),我們的Maven項目要以這個項目爲父工程,這樣我們就不用操心依賴的版本問題。

6)pom.xml中引入Web啓動器包

    <dependencies>
        <!--SpringMVC Web的啓動器,會自動引入很多的Maven包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

這個依賴添加後,Maven加載結束後,會發現項目中引入了很多的包。

7)創建啓動類

package rui;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/*啓動類*/
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

springBoot的程序啓動需要通過該啓動類,雖然是Web項目,無需通過Tomcat運行,其內部內置了Tomcat的運行環境,直接運行main函數即可。

8)編寫Controller

package rui.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "hello")
public class HelloController {

    @RequestMapping(value = "index")
    public String hello()
    {
        return "hello, spring boot!";
    }
}

9)添加yaml配置文件,是一直新的配置文件格式,替代了以前的application.properties,當然application.properties也是可以繼續使用的。application.properties的優先級高。

10)運行程序並測試

 

 

 

 

5、SpringBoot熱部署

1)pom.xml加入依賴包

        <!--熱啓動的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

2)pom.xml增加build配置

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

 3)application.yaml文件配置

#熱部署
spring:
  devtools:
    restart:
      enabled: true

4)Idea配置

 

 

 

 

 

 修改文件保存後,控制檯會自動輸入如下的內容,類似自動啓動了一下。

 

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