1. 程式人生 > 其它 >springboot學習一 簡單入門

springboot學習一 簡單入門

1.1、搭建專案

1、建立一個空的maven專案

2、pom.xml 加入springboot父標籤

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.3</version>
</parent>

3、引入web依賴

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

4、編寫啟動程式

package com.tuling.springboot;
​
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);
    }
}
​

5、編寫業務程式碼

package com.tuling.springboot.controller;
​
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
​
@Controller
@RequestMapping("/hello")
public class HelloWorldController {
​
    @RequestMapping("/world")
    @ResponseBody
    public String helloWorld(){
        return "hello world!";
    }
}
​

1.2、打包並執行

1、打包

2、執行

執行報錯。

3、包結構

4、MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: springboot-pro
Implementation-Version: 1.0-SNAPSHOT
Build-Jdk-Spec: 1.8
Created-By: Maven Jar Plugin 3.2.0
​

1.3、引入外掛

1、加入外掛

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2、打包

mvn package

注意:打包時,已經存在的jar檔案不能被別的檔案開啟,如果採用idea整合工具打包,錯誤無法真實的暴露出來,可以在terminal(終端)模式下采用命令列方式打包,會比較清晰的暴露出錯誤

3、引入外掛後的包結構

4、包中MANIFEST.MF的內容

Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: springboot-pro
Implementation-Version: 1.0-SNAPSHOT
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
Start-Class: com.tuling.springboot.Application
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.5.3
Created-By: Maven Jar Plugin 3.2.0
Main-Class: org.springframework.boot.loader.JarLauncher

上述內容指定了啟動類:Start-Class: com.tuling.springboot.Application

1.4、指定配置

1、加入application.properties配置檔案

server.port=8080
server.servlet.context-path=/

1.5、程式解釋

1.5.1、SpringBootApplication註解

註解程式碼如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {.......}

可以發現它是由眾多註解組合而成的,下面具體分析下這裡每個註解所起到的作用。

  • @Target Target通過ElementType來指定註解可使用範圍的列舉集合(FIELD/METHOD/PARAMETER...)

  • @Retention Retention(保留)註解說明,這種型別的註解會被保留到那個階段. 有三個值:

    • RetentionPolicy.SOURCE —— 這種型別的Annotations只在原始碼級別保留,編譯時就會被忽略

    • RetentionPolicy.CLASS —— 這種型別的Annotations編譯時被保留,在class檔案中存在,但JVM將會忽略

    • RetentionPolicy.RUNTIME —— 這種型別的Annotations將被JVM保留,所以他們能在執行時被JVM或其他使

  • @Documented 註解表明這個註解應該被 javadoc工具記錄. 預設情況下,javadoc是不包括註解的. 但如果宣告註解時指定了 @Documented,則它會被 javadoc 之類的工具處理, 所以註解型別資訊也會被包括在生成的文件中

  • @Inherited 允許子類繼承父類的註解,僅限於類註解有用,對於方法和屬性無效。

  • @SpringBootConfiguration 註解實際上和@Configuration有相同的作用,配備了該註解的類就能夠以JavaConfig的方式完成一些配置,可以不再使用XML配置。

  • @ComponentScan 這個註解完成的是自動掃描的功能,相當於Spring XML配置檔案中的:<context:component-scan>,可使用basePackages屬性指定要掃描的包,及掃描的條件。如果不設定則預設掃描@ComponentScan註解所在類的同級類和同級目錄下的所有類,所以我們的Spring Boot專案,一般會把入口類放在頂層目錄中,這樣就能夠保證原始碼目錄下的所有類都能夠被掃描到。

  • @EnableAutoConfiguration 這個註解是讓Spring Boot的配置能夠如此簡化的關鍵性註解。

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import(AutoConfigurationImportSelector.class)
    public @interface EnableAutoConfiguration {
    • @AutoConfigurationPackage 註解用於儲存自動配置類以供之後的使用,比如給JPA entity掃描器,用來掃描開發人員通過註解@Entity定義的entity類。通俗的講就是,註冊bean定義到容器中。

    • @Import(AutoConfigurationImportSelector.class)是EnableAutoConfiguration註解中最關鍵的來,它藉助AutoConfigurationImportSelector,可以幫助SpringBoot應用將所有符合條件的@Configuration配置都載入到當前SpringBoot建立並使用的IoC容器中。

1.5.2、版本仲裁中心

在pom.xml檔案中繼承了一個父專案:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
    </parent>

在spring-boot-starter-parent專案中又繼承了一個父專案:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.5.3</version>
  </parent>

在spring-boot-dependencies專案中儲存了springboot需要的一些依賴包的版本資訊,因此稱spring-boot-dependencies為版本仲裁中心

1.5.3、場景啟動器

在我們建立的專案中,還存在一個依賴spring-boot-starter-web:

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

稱這個依賴為場景啟動器。springboot把常用的開發場景進行了總結,做成各種不同的pom檔案,可以方便各種不同場景的快速環境的搭建。故稱為場景啟動器。

所有的官方啟動器都遵循一個命名規則。 "spring-boot-starter-*",其中*程式碼一個特定型別的應用。
第三方的啟動器不能以"spring-boot-starter-"開頭,因為這是為官方保留的spring專案。相反第三方的啟動器一般以專案名稱開頭。一般名稱格式是 "第三方專案名稱-spring-boot-starter"