年輕人的第一個自定義 Spring Boot Starter!

  • 2019 年 10 月 5 日
  • 筆記

陸陸續續,零零散散,棧長已經寫了幾十篇 Spring Boot 系列文章了,其中有介紹到 Spring Boot Starters 啟動器,使用的、介紹的都是第三方的 Starters ,那如何開發一個自己的 Spring Boot Starter 呢?

下面帶大家開發一個年輕人的第一個 Spring Boot Starter!

不知道 Starters 為何物的請進這個傳送門===> Spring Boot Starters 啟動器,看完有了學習基礎,我們再繼續下面的自定義 Starter 實戰!

一、自定義 Starter 必備組件

一個完整的 Spring Boot Starter 需要包含以下組件:

  • 包含自動配置程式碼的自動配置模組;參考:Spring Boot自動配置原理、實戰。
  • Starter模組提供對自動模組的依賴關係,和相關依賴庫,以及任何需要用到的依賴。簡而言之,就是,添加一個 Starter 就應該提供使用該 Starter 所需的一切;

二、創建一個自定義Starter

怎麼創建 Spring Boot 項目就不說了,之前也分享過,參考:年輕人的第一個 Spring Boot 應用!。

這個自定義 Starter 就實現一個根據屬性的值是否配置Bean。

1、創建自動配置類

package cn.javastack.springboot.starter.config;    import cn.javastack.springboot.starter.service.TestService;  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;  import org.springframework.context.annotation.Bean;  import org.springframework.context.annotation.Configuration;    @Configuration  @ConditionalOnProperty(prefix = "javastack.starter", name = "enabled", havingValue = "true")  public class TestServiceAutoConfiguration {        @Bean      public TestService testService() {          return new TestService();      }    }  

這個自動配置類很簡單,就是根據是否有 javastack.starter.enabled=true 這個參數的值再配置一個Bean。

TestService示例如下:

package cn.javastack.springboot.starter.service;    public class TestService {        public String getServiceName() {          return "Java技術棧";      }    }  

這個類就有一個方法 getServiceName,它就返回一個字元串:Java技術棧。

2、允許自動配置

創建 META-INF/spring.factories 文件,添加這個允許自動配置的類。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=  cn.javastack.springboot.starter.config.TestServiceAutoConfiguration  

三、測試這個自定義Starter

上面的自定義 Starter 項目建好後,可以來測試一下它是否生效了。

一般是把它打成 jar 包上傳到 Maven 倉庫,供其他同事調用,這裡我們本報打完包之後再引用它。

1、添加依賴

新建一個 Spring Boot 測試項目,添加這個自定義 Starter 依賴,Maven 依賴如下:

<dependencies>      <dependency>          <groupId>cn.javastack</groupId>          <artifactId>javastack-spring-boot-starter</artifactId>          <version>1.0</version>          <scope>compile</scope>      </dependency>  </dependencies>  

2、添加測試方法

package cn.javastack.springboot.starter.sample;    import cn.javastack.springboot.starter.service.TestService;  import org.springframework.boot.CommandLineRunner;  import org.springframework.boot.SpringApplication;  import org.springframework.boot.autoconfigure.SpringBootApplication;  import org.springframework.context.annotation.Bean;    /**   * 微信公眾號:Java技術棧   */  @SpringBootApplication  public class Application {        public static void main(String[] args) {          SpringApplication.run(Application.class);      }        @Bean      public CommandLineRunner commandLineRunner(TestService testService) {          return (args) -> {              System.out.println(testService.getServiceName());          };      }    }  

這個方法的作用是,項目啟動後,調用 TestService 的實例方法,輸出方法的值。

關於 CommandLineRunner 有不懂的可以看這篇文章:Spring Boot Runner 啟動器。

3、開啟配置

我們知道這個自定義 Starter 中需要有 javastack.starter.enabled=true 這個參數的值的,所以我們在 application.yml 配置文件中添加這個配置:

javastack:    starter:      enabled: true  

4、運行測試

運行 Application 類的 main 方法,最後會輸出結果:Java技術棧。

當我們把配置改為:

javastack:    starter:      enabled: false  

此時,運行報錯,因為沒有這個實例啊,自動配置類只有為 true 時才會配置。

四、總結

本章棧長簡單演示了如何自定義一個 Spring Boot Starter,根據某個參數的值來決定是否自動配置,其實還可以根據是否有某個類、某個Bean……等,可以看下《Spring Boot 最核心的 25 個註解》這篇文章對應的 ConditionOnXXX 系列註解。

其實了解了 Spring Boot 自動配置的原理,自定義一個 Starter 並不難,你可以在這個實例基礎上靈活擴展。

本文完整的程式碼實例 Github 地址:

https://github.com/javastacks/spring-boot-best-practice

趕緊關注加星吧,已經更新一大堆教程了,後續這個教程會持續更新……

Tags: