如何定制自己的起步依賴(lài) Spring Boot Starter

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


在這里我們一起動(dòng)手實(shí)現(xiàn)一個(gè)屬于自己的起步依賴(lài)

代碼托管于 GitHub,歡迎 Star :kissing_heart:

主要內(nèi)容

主要加入兩個(gè)模塊,一個(gè)是與自動(dòng)配置相關(guān)的模塊,如果你的依賴(lài)需要做自動(dòng)配置,那么我們可以在里面寫(xiě)上自動(dòng)配置。另一個(gè)是 starter 模塊,它里面就是一些依賴(lài)項(xiàng),首先就是指向我們的 autoconfigure 模塊的一個(gè)依賴(lài),另外就是當(dāng)前這個(gè) Starter 所自己需要的依賴(lài)項(xiàng)。

  • autoconfigure 模塊,包含自動(dòng)配置代碼
  • starter 模塊,包含指向自動(dòng)配置模塊的依賴(lài)及其他相關(guān)依賴(lài)

這里說(shuō)明一下,autoconfigure 并不是必須的,如果當(dāng)前這個(gè)模塊并不需要什么自動(dòng)配置,就可以把它去掉。而 Spring Boot 相關(guān)的那些自動(dòng)配置很多都是集中在 spring-boot-autoconfigure 里面的,所以它只要依賴(lài)了 spring-boot-starter,那么就會(huì)自動(dòng)地加入這些 Autoconfigure。

命名方式

一般是建議在前面加上一個(gè)前綴,主要是與 Spring Boot 官方的那些依賴(lài)做區(qū)分,如下所示:

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

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

一些注意事項(xiàng)

  • 不要使用 spring-boot 作為依賴(lài)的前綴

    如果你這樣做了,會(huì)和 Spring Boot 官方的那些依賴(lài)混在一起,從而導(dǎo)致不好辨認(rèn)。

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

    另外如果你有一些配置,這里也是建議不要使用 Spring Boot 已經(jīng)在使用的配置命名空間。比方說(shuō)它里面有 servermanagement 相關(guān)的這些配置項(xiàng),那么你就不要再使用以 server、management 命名前綴的配置了。

  • starter 中僅添加必要的依賴(lài)

    在當(dāng)前這個(gè) Starter 中只加入必要的依賴(lài)就可以了。也許這個(gè)要求有點(diǎn)苛克,但這里的建議是希望你的依賴(lài)不多不少、正正好好,你要使用到哪些依賴(lài)就只加這些依賴(lài)就好;如果沒(méi)有必要加進(jìn)去的就可以去掉它,這樣的好處是可以減少最終打出來(lái)的包里面的依賴(lài)。

  • 聲明對(duì) spring-boot-starter 的依賴(lài)

    如果有需要的可以在這個(gè) Starter 當(dāng)中加入 spring-boot-starter 這個(gè)依賴(lài)。這個(gè)并不是必須的,因?yàn)槲覀儸F(xiàn)在很多的工程本身就是 spring-boot 的一個(gè)項(xiàng)目,所以它本身就添加了對(duì) spring-boot-starter 的依賴(lài)。這個(gè)要看你的需要來(lái)決定一下是否要添加。


擼起袖子加油干

下面我們來(lái)看看都有哪些方式可以實(shí)現(xiàn)自動(dòng)配置

  • 傳統(tǒng)手工實(shí)現(xiàn)的自動(dòng)配置(見(jiàn) custom-starter-spring-lt4-autoconfigure

    :在低版本的 Spring 中能使用這種方式快速實(shí)現(xiàn)類(lèi)似自動(dòng)配置的功能。

    <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 的自動(dòng)配置(見(jiàn) 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(見(jiàn) custom-starter-spring-boot-starter

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

運(yùn)行 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!

以上就是一個(gè)簡(jiǎn)單的 Starter,在里面加入自己的自動(dòng)配置和相關(guān)的依賴(lài)。那么到這里你也可以實(shí)現(xiàn)一個(gè)屬于你的 Starter,從而簡(jiǎn)化你的 Maven 依賴(lài)項(xiàng)。


參考鏈接

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容