Spring Boot Starter 公式サイトの説明: Spring Boot Starter 公式紹介
Spring Boot スターターとは
スターターは、アプリケーションに統合できる一連の依存パッケージを含むスターターとして理解することができ、サンプル コードや依存パッケージをあちこちで探すことなく、Spring やその他のテクノロジをワンストップで統合できます。Spring Boot Starter の動作原理は次のとおりです。Spring Boot は、プロジェクトが起動時に依存する JAR パッケージをスキャンし、spring.factories ファイルを含む JAR パッケージを探し、構成に従ってクラスをspring.factories
ロードし、自動構成を実行し、Bean を注入します。アノテーション条件によるAutoConfigure
@Conditional
Spring Context
Spring Boot Starter をカスタマイズする理由
開発を簡単にするために、Spring Boot の公式 Web サイトには多くのスターターが用意されていますが、それでも実際の作業での開発シナリオを完全に満たすことはできず、カスタム実装のスターターをカスタマイズする必要があります。 .
実装手順
1. まず、Maven の空のプロジェクトを作成し、2 つのモジュールを追加します。
ランチャー
ランチャーにソース コードはありません。現在のシーンにどの依存関係を導入する必要があるかを教えてください。
xiaozhao-hello-spring-boot-starter
対応する依存ファイルであるという名前の Maven プロジェクトとしてランチャー モジュールを作成します。
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
自動構成パッケージ
すべての自動構成機能は、自動構成パッケージに実装されています!
という名前の SpringBoot 初期化プロジェクトとして自動構成パッケージ モジュールを作成します。xiaozhao-hello-spring-boot-starter-autoconfigure
最終的なプロジェクト モジュールは次のとおりです。
2. モジュールが作成されたら、自動構成モジュールをランチャーにインポートする必要があります (他の誰かがシーン ランチャーを導入すると、自動構成パッケージが自動的にインポートされます)。
<dependencies>
<dependency>
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3. 自動構成モジュールを作成します。
- カスタム プロパティ ファイルを作成する
@ConfigurationProperties("xiaozhao.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
- プロパティ ファイルの値を読み取るビジネス クラスを作成します。
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String userName){
return helloProperties.getPrefix() + ":" + userName + ">" + helloProperties.getSuffix();
}
}
- 別の自動構成クラス、自動クラス読み込み
@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
return helloService;
}
}
最終的な効果は次のとおりです。
4.ディレクトリにMETA-INF/spring.factoriesresources
を作成し、以下の構成情報を追加します。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhao.hello.auto.HelloServiceAutoConfiguration
5.hello-spring-boot-starter-autoconfigure
モジュールを取り付け、xiaozhao-hello-spring-boot-starter
6. インストールが完了したら、作成したスターターを導入するための新しいプロジェクトを作成します (このプロジェクトをhello-test
Spring Boot 初期化プロジェクトと呼びます)。
前に定義した Starter スターターと、Spring Boot に付属する Web スターターを紹介します。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zhao</groupId>
<artifactId>xiaozhao-hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
7.hello-test
プロジェクトにテスト コントローラーを作成する
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello() {
String str = helloService.sayHello("李四");
return str;
}
}
設定ファイルを書く
xiaozhao.hello.prefix=hello
xiaozhao.hello.suffix=666
8. プロジェクトを開始してテストする
カスタム Starter の実装ロジックをまとめる
- 最初にカスタム スターター
xiaozhao-hello-spring-boot-starter
を導入します: 、このスターターでカスタム自動構成シナリオを導入します - 自動構成シーンが開始されると、spring.factories ファイルが検索され、
HelloServiceAutoConfiguration
クラスファイルが自動的に読み込まれます。 - 自動構成クラスがロードされた後、
@ConditionalOnMissingBean(HelloService.class)
このアノテーションを介して、コンテナーにコンポーネントがない場合HelloService
、コンポーネントが自動的に追加されHelloService
ます。 HelloService
コンポーネントのすべてのプロパティは、バインディングによってHelloPropertie
s 構成ファイル@ConfigurationProperties("xiaozhao.hello")
を通じてxiaozhao.hello。xxx
バインドされます。- 自分でコンテナにコンポーネントを注入すると、
HelloService
自動的に構成されず、再注入されます。