1. 程式人生 > >spring-boot入門程式詳解

spring-boot入門程式詳解

1.建立一個普通的maven專案,專案名為boot-learnning

2.在pom.xml新增parent依賴

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

3.在pom.xml中的dependencies中新增依賴

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

由於在parent中已經指定了版本,所以在dependencies中關於spring-boot的依賴就不需要版本了,如果要引入其他的,仍然需要指定版本

4.編寫main方法程式,用於啟動應用

import org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }

直接執行main方法程式,就啟動了一個web引用。spring-boot中預設內嵌了tomcat,所以只要執行主程式,就啟動了內嵌的tomcat,並預設監聽8080

5.編寫一個controller,用於客戶端訪問

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {


    @RequestMapping("/index")
    @ResponseBody
    public String index() {
        return "Spring boot index Controller";
    }
}

瀏覽器中訪問http://localhost:8080/index,我們就能在瀏覽器中看到返回的內容

6.在pom.xml中新增spring-boot編譯外掛

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

7.編譯打包

mvn clean compile package

編譯打包完成後,在專案的target目錄下,就會生成一個jar檔案

8.部署啟動

java -jar boot-learnning-0.0.1-SNAPSHOT.jar

啟動後,在瀏覽器中訪問http://localhost:8080/index,在瀏覽器中,就能看到controller返回的內容

9.依賴探究

我們在搭建環境的時候添加了父級依賴

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

而在spring-boot-starter-parent中,也有一個父級依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

spring-boot-dependencies中,properties定義了我們常用的的元件版本,幫我們管理spring-boot中需要的所有依賴

那麼在我們開發的過程中,我們在匯入spring-boot-dependencies中已經宣告的依賴庫時,就不需要指定版本了,如果在spring-boot-dependencies沒有指定,就需要我們來指定版本了
dependencies,我們依賴了spring-boot-starter-web

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

spring-boot-starterspring-boot的場景啟動器,幫我們管理匯入了web模組正常執行所需要的依賴庫
spring-boot 將所有的功能場景都抽取出來,做成一個個的starter(啟動器),只需要在專案中引入這些starter,相關場景的所有依賴都會自動匯入,我們需要使用什麼功能,就匯入相應的場景的啟動器依賴即可

10.主程式探究

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

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
       SpringApplication.run(App.class, args);
    }
}

@SpringBootApplication 標註在某個類上,說明這個類是spring-boot應用的主配置類,spring-boot就會通過這個類來啟動spring-boot應用

SpringApplicationrun方法中,第一個引數,就是一個使用@SpringBootApplication的類,而這個類也可以不是當前main方法所在的類

11.@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 {
  • @SpringBootConfiguration 標註的類為一個spring-boot應用的配置類
  • @Configuration 標註一個類,使其成為一個配置類
  • @EnableAutoConfiguration 開啟自動配置,表示原來我們使用spring時,需要手動配置的東西,現在由spring-boot幫我們自動配置,這個註解告訴spring-boot開啟自動配置功能,是的自動配置功能生效

配置類 與 配置檔案是一樣的,而配置類也是容器中的一個元件

12.@EnableAutoConfiguration 探究

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
  • @AutoConfigurationPackage 自動配置包,它使用@Import(AutoConfigurationPackages.Registrar.class) 來完成所需的功能
  • @Importspring 的一個底層註解,給容器中匯入一個元件;匯入的元件由AutoConfigurationPackages.Registrar.class類指定
  • AutoConfigurationPackages.Registrar.class 將主配置類(@SpringBootApplication標註的類)的所在包,以及下面的所有子包裡面的所有元件掃描到spring容器中
  • @Import(AutoConfigurationImportSelector.class) 中的AutoConfigurationImportSelector.class 是一個確定匯入哪些元件的選擇器,將所有需要的元件以全類名的方式返回,然後將這些元件全部新增到容器中,還會給容器中匯入非常多的自動配置類,即向容器中匯入各個場景需要的所有元件,並且自動配置好這些元件

13.自動配置類

自動配置類,免去了我們手動編寫配置注入功能元件等工作

AutoConfigurationImportSelectorselectImports方法中,呼叫了getCandidateConfigurations方法獲取候選的配置檔案,使用了.SpringFactoriesLoader.loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader),其中,第一個引數就是EnableAutoConfiguration.class,spring-boot應用在啟動的時候,呼叫loadFactoryNames方法從類路徑下的META-INF/spring.factories獲取EnableAutoConfiguration指定的值,將這些值作為自動配置類,匯入到容器中,自動配置類生效,幫我們進行自動配置工作,而這個檔案存在於spring-boot-autoconfigure-2.0.0.RELEASE.jar中。

以前我們需要自己手動配置的東西,自動配置類都幫我們做了,spring-boot底層中,關於spring-framework中的東西,並沒有缺少或者省略,只不過是由自動配置類幫我們設定了,J2EE的整體解決方案的自動配置類,都在spring-boot-autoconfigure中。