我的第一個springboot starter

  在springboot中有很多starter,很多是官方開發的,也有是個人或開源組織開發的。這些starter是用來做什麼的吶?

一、認識starter

  所謂的starter,在springboot中其實是一個依賴,是springboot官方爲了方便開發者更好的管理依賴而開發的,像前邊提到的mybatis的starter,

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
</dependency>

  這是一個在springboot中使用mybatis的starter,看下其包含了哪些內容,下面是該starter中包含的依賴,

更有意思的一點是mybatis-spring-boot-starter沒有代碼,

也就是說mybatis-spring-boot-starter不具備實際功能,僅僅提供所需的依賴。那麼是如何把mybatis相關的內容注入到springboot中的吶,是因爲mybatis中有兩位一個依賴”mybatis-spring-boot-autoconfigure“,該依賴中有有關自動配置的相關內容,先混個眼熟,

其中,META-INF下的spring.factories和MybatisAutoConfiguration兩個文件是重點,後面會重點分析springboot自動配置的原理。

現在,現在對starter有了一個清析的認識,starter提供一個組件集成springboot的入口,包含了需要的依賴以及自動配置類

下面,看下springboot提供了哪些starter。

二、starter彙總

2.1、spring-boot-starter

其依賴爲,

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

2.2、spring-boot-starter-web

這個是開發springboot web項目時的starter,

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

2.3、spring-boot-starter-jdbc

和jdbc相關的,其依賴爲

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

2.4、spring-boot-starter-activemq

和activeMQ相關的

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

2.5、spring-boot-starter-data-elasticsearch

和ES相關的

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

2.6、spring-boot-starter-data-jpa

和JPA相關的

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

2.7、spring-boot-starter-data-mongodb

和mongodb相關的

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

2.8、spring-boot-starter-quartz

和定時任務相關的

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

 2.9、mybatis-spring-boot-starter

和mybatis相關的

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

 

  還有很多starter,這裏暫且列舉那麼多,有個名稱上的區別大家發現沒,有的是”spring-boot-starter-XX“,有的是”XX-spring-boot-starter“,對於前者可以理解爲是springboot官方提供的,對於後者可以理解爲是第三方自己開發,我們如果自己開發一個其命名規範最好遵循後者。

三、自定義starter

  前邊說了很多有關官方的starter,現在自定義一個starter,有如下要求,該starter的名字暫定爲:”customer-spring-boot-starter“,其包含一個依賴”customer-spring-boot-autoconfigurer“,在”customer-spring-boot-autoconfigurer“中會讀取前綴爲”my.customer“的配置文件,並且把MyCustomerService注入到springboot中,現在看要如何自定義一個starter。

3.1、customer-spring-boot-starter

  從”mybatis-spring-boot-starter“知道,XX-spring-boot-starter是一個空項目,在該項目中會依賴XX-spring-boot-autoconfigurer,下面新建空項目”customer-spring-boot-starter“,

對於src目錄完全可以刪除,這裏暫時保留,重點看下pom.xml文件,我這裏僅依賴了”customer-spring-boot-autoconfigurer“,

<?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>org.example</groupId>
    <artifactId>customer-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--自動配置模塊-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>customer-spring-boot-autoconfigurer</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

”customer-spring-boot-starter“就這樣了,打包安裝到本地倉庫即可。

3.2、customer-spring-boot-autoconfigurer

  相對於”customer-spring-boot-starter“來說,”custoerm-spring-boot-autoconfigurer“要複雜一些。建一個普通的maven項目即可,

先看MyProperties文件吧,該文件負責讀取配置文件(application.properties)文件中前綴爲”my.customer“的配置,

MyProperties.java

package com.my.autoconfigurer;

import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * 讀取配置文件中的配置
 * 讀取前綴爲my.customer的配置
 * @date 2022/6/12 17:06
 */
@ConfigurationProperties(prefix = "my.customer")
public class MyProperties {
    private String name;
    private String code;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}

該類中使用了@ConfigurationProperties註解用來讀取配置文件,所以要引入spring-boot-autoconfigure的依賴,後邊pom.xml會體現。下面看要注入的服務,

MyCustomerService.java

package com.my.autoconfigurer;

/**
 * 自定義的服務類
 * @date 2022/6/12 17:05
 */
public class MyCustomerService {
    //MyProperties的引用
    private MyProperties myProperties;

    public MyProperties getMyProperties() {
        return myProperties;
    }
    public void setMyProperties(MyProperties myProperties) {
        this.myProperties = myProperties;
    }
    //自定義方法
    public void myCustomerMethod(){
        System.out.println("name:"+myProperties.getName()+",code:"+myProperties.getCode());
    }
}

這個類也很簡單,有一個MyProperties的屬性,另外有一個自定義方法myCustomerMethod方法。主角要登場了,自動配置類,

MyCustomerAutoconfiguration.java

package com.my.autoconfigurer;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自定義的自動配置類
 * @date 2022/6/12 17:11
 */
@Configuration
@EnableConfigurationProperties(MyProperties.class)
public class MyCustomerAutoconfiguration {
   //向springboot的容器中注入名爲"myCustomerService"的實例
    @Bean
    public MyCustomerService myCustomerService(MyProperties myProperties){
        MyCustomerService myCustomerService=new MyCustomerService();
        myCustomerService.setMyProperties(myProperties);
        return myCustomerService;
    }
}

上面就是一個簡單的自定義配置類。配置完這些完了嗎,還沒有,重量級選手登場,在src/resources下新建”META-INF“文件夾,且在下面新建文件”spring.factories“,

該文件的內容爲,

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.my.autoconfigurer.MyCustomerAutoconfiguration

配置這樣一個key-value即可,key只能爲”org.springframework.boot.autoconfigure.EnableAutoConfiguration“,value就是自定義的配置類,如果有多個使用”,“分割即可。另外該文件的名稱也只能是”spring.factories“

上面的這些配置完成,最後看下pom.xml文件,

 

<?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>org.example</groupId>
    <artifactId>customer-spring-boot-autoconfigurer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
    <!--springboot的自動配置依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
    </dependencies>
</project>

至此,自定義自動配置完成,打包安裝到本地倉庫即可。

3.3、使用customer-spring-boot-starter

  上面已經完成了第一個自定義的starter,到了真金驗證的時候了,

導入”customer-spring-boot-starter“依賴,

寫個測試類,測試下是否自動導入了”MyCustomerService“類,

 

package com.my.template.controller;

import com.my.autoconfigurer.MyCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 測試自動配置類
 * @date 2022/6/12 17:46
 */
@Controller
public class MyController {
    //自動注入的類
    @Autowired
    private MyCustomerService myCustomerService;

    @RequestMapping("test")
    @ResponseBody
    public void test(){
        myCustomerService.myCustomerMethod();
    }
}

另外在application.properites文件中配置自定義的配置文件,

#自定義配置
my.customer.name=hello
my.customer.code=autoconfiguration

啓動服務,看下打印結果,

可以看到打印的和配置的是一樣的,證明自定義的自動配置類成功,就是說自定義starter成功。

四、總結

  本文從”mybatis-spring-boot-starter“開始,分析了一個starter的組成部分,然後總結了常用的starter,重點是實現了一個自定義的starter,併成功獲取到自定義的配置。

  1、starter包含要注入的類的依賴及自定配置模塊(XX-spring-boot-autoconfigure);

  2、自動配置模塊(XX-spring-boot-autoconfigure)包含自動配置類及spring.factories文件;

 

  不知道小夥伴對springboot如何實現自動配置好奇嗎,我們下期見。

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