y0ngb1n

Aben Blog

欢迎来到我的技术小黑屋ヾ(◍°∇°◍)ノ゙
github

自分の起動依存関係を Spring Boot Starter にカスタマイズする方法

ここでは、自分だけのスタート依存関係を実現するために一緒に作業します

コードは GitHub にホストされています。スターを歓迎します 😘

主要内容#

主に 2 つのモジュールを追加します。1 つは自動構成に関連するモジュールで、依存関係が自動構成を必要とする場合は、その中に自動構成を記述できます。もう 1 つは starter モジュールで、そこにはいくつかの依存項目が含まれています。まず、私たちの autoconfigure モジュールへの依存を指し、次にこの Starter 自体が必要とする依存項目です。

  • autoconfigure モジュール、自動構成コードを含む
  • starter モジュール、自動構成モジュールへの依存およびその他の関連依存を含む

ここで説明しておくと、autoconfigure は必須ではありません。現在のモジュールが自動構成を必要としない場合は、それを削除できます。また、Spring Boot に関連する自動構成は多くが spring-boot-autoconfigure に集中しているため、spring-boot-starter に依存していれば、これらの自動構成が自動的に追加されます。

命名方式#

一般的には、前にプレフィックスを追加することをお勧めします。これは、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      : みんなこんにちは!私たちは皆Springが好きです!

以上がシンプルな Starter であり、その中に自分の自動構成と関連する依存関係を追加しました。これで、あなた自身の Starter を実現し、Maven の依存関係を簡素化できます。


参考リンク#

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。