如何定製自己的起步依賴 Spring Boot Starter

感謝您的閱讀,本文由 楊斌的博客 版權所有。
如若轉載,請註明出處:楊斌的博客(https://y0ngb1n.github.io/a/coustomize-your-own-spring-boot-starter.html


在這裏我們一起動手實現一個屬於自己的起步依賴

代碼託管於 GitHub,歡迎 Star :kissing_heart:

主要內容

主要加入兩個模塊,一個是與自動配置相關的模塊,如果你的依賴需要做自動配置,那麼我們可以在裏面寫上自動配置。另一個是 starter 模塊,它裏面就是一些依賴項,首先就是指向我們的 autoconfigure 模塊的一個依賴,另外就是當前這個 Starter 所自己需要的依賴項。

  • autoconfigure 模塊,包含自動配置代碼
  • starter 模塊,包含指向自動配置模塊的依賴及其他相關依賴

這裏說明一下,autoconfigure 並不是必須的,如果當前這個模塊並不需要什麼自動配置,就可以把它去掉。而 Spring Boot 相關的那些自動配置很多都是集中在 spring-boot-autoconfigure 裏面的,所以它只要依賴了 spring-boot-starter,那麼就會自動地加入這些 Autoconfigure。

命名方式

一般是建議在前面加上一個前綴,主要是與 Spring Boot 官方的那些依賴做區分,如下所示:

  • xxx-spring-boot-autoconfigure
  • xxx-spring-boot-starter

這樣就可以定義一個你自己的 Spring Boot Starter 了。

一些注意事項

  • 不要使用 spring-boot 作爲依賴的前綴

    如果你這樣做了,會和 Spring Boot 官方的那些依賴混在一起,從而導致不好辨認。

  • 不要使用 spring-boot 的配置命名空間

    另外如果你有一些配置,這裏也是建議不要使用 Spring Boot 已經在使用的配置命名空間。比方說它裏面有 servermanagement 相關的這些配置項,那麼你就不要再使用以 servermanagement 命名前綴的配置了。

  • starter 中僅添加必要的依賴

    在當前這個 Starter 中只加入必要的依賴就可以了。也許這個要求有點苛克,但這裏的建議是希望你的依賴不多不少、正正好好,你要使用到哪些依賴就只加這些依賴就好;如果沒有必要加進去的就可以去掉它,這樣的好處是可以減少最終打出來的包裏面的依賴。

  • 聲明對 spring-boot-starter 的依賴

    如果有需要的可以在這個 Starter 當中加入 spring-boot-starter 這個依賴。這個並不是必須的,因爲我們現在很多的工程本身就是 spring-boot 的一個項目,所以它本身就添加了對 spring-boot-starter 的依賴。這個要看你的需要來決定一下是否要添加。


擼起袖子加油幹

下面我們來看看都有哪些方式可以實現自動配置

  • 傳統手工實現的自動配置(見 custom-starter-spring-lt4-autoconfigure

    :在低版本的 Spring 中能使用這種方式快速實現類似自動配置的功能。

    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
      </dependency>
      <dependency>
        <groupId>io.github.y0ngb1n.samples</groupId>
        <artifactId>custom-starter-core</artifactId>
        <scope>provided</scope>
      </dependency>
    </dependencies>
    
  • 基於 Spring Boot 的自動配置(見 custom-starter-spring-boot-autoconfigure

    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
      </dependency>
      <dependency>
        <groupId>io.github.y0ngb1n.samples</groupId>
        <artifactId>custom-starter-core</artifactId>
        <scope>provided</scope>
      </dependency>
    </dependencies>
    
  • 引用自定義 Starter(見 custom-starter-spring-boot-starter

    <dependencies>
      <dependency>
        <groupId>io.github.y0ngb1n.samples</groupId>
        <artifactId>custom-starter-spring-boot-starter</artifactId>
      </dependency>
    </dependencies>
    

運行 custom-starter-examples 效果如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2019-05-02 23:15:56.183  INFO 17236 --- [           main] i.g.y.s.d.AutoconfigureDemoApplication   : Starting AutoconfigureDemoApplication on HP with PID 17236 ...
2019-05-02 23:15:56.208  INFO 17236 --- [           main] i.g.y.s.d.AutoconfigureDemoApplication   : No active profile set, falling back to default profiles: default
2019-05-02 23:15:57.198  INFO 17236 --- [           main] i.g.y.s.g.GreetingApplicationRunner      : Initializing GreetingApplicationRunner.
2019-05-02 23:15:57.478  INFO 17236 --- [           main] i.g.y.s.d.AutoconfigureDemoApplication   : Started AutoconfigureDemoApplication in 2.516 seconds (JVM running for 5.501)
2019-05-02 23:15:57.486  INFO 17236 --- [           main] i.g.y.s.g.GreetingApplicationRunner      : Hello everyone! We all like Spring!

以上就是一個簡單的 Starter,在裏面加入自己的自動配置和相關的依賴。那麼到這裏你也可以實現一個屬於你的 Starter,從而簡化你的 Maven 依賴項。


參考鏈接

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