SpringBoot自動構成:スターターステータスをカスタマイズする

カスタムスターターを作成します。

Springbootの登場により、開発者の構成が大幅に簡素化され、優れたツールの1つは、springbootのコアコンポーネントであるspringbootのスターターです。公式のspringbootは、開発者のさまざまな利便性もカプセル化します。使用されるスターターモジュールは次のとおりです。

  • spring-boot-starter-web // springMVC相関連
  • spring-boot-starter-aop //アスペクトプログラミング関連
  • spring-boot-starter-cache //キャッシュ関連

(1)カスタマイズされたスターター要件:
公式Webサイトでは、2つのモジュールを作成する必要があります。1つは自動構成モジュールで、もう1つはスターターモジュールです。スターターモジュールは、主に推移的な依存関係として機能する自動構成モジュールに依存します(省略可能) )。

(2)命名規則:
公式スターターspring-boot-starter-xxxはフォーマットで名前が付けられサードパーティの開発者によってカスタマイズされたスターターxxxx-spring-boot-starterはルールで名前が付けられます。

1自動構成モジュールを作成します

最初に空のプロジェクトを作成し、次にSpringBootモジュールを作成して、このクラスのスタートアップクラスとテストクラスを削除します。
ここに画像の説明を挿入

(1)ポンポン構成

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

これらの2つの依存関係のみがpomファイルに保持されます。その中にspring-boot-starterは、ランチャーの基本的な依存関係がありspring-boot-configuration-processor、ファイルの構成時にプロンプ​​トを表示できます。
ここに画像の説明を挿入

(2)構成の書き込み

package com.glp.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "diy.hello")
public class Properties {
    
    
    private String pre;
    private String suf;

    public String getPre() {
    
    
        return pre;
    }

    public void setPre(String pre) {
    
    
        this.pre = pre;
    }

    public String getSuf() {
    
    
        return suf;
    }

    public void setSuf(String suf) {
    
    
        this.suf = suf;
    }
}

@ConfigurationProperties:この構成クラスはapplication.properties/application.yaml、プロパティインジェクション用にSpringBootの構成ファイルに関連付けられてます。

(3)ライティングサービス

package com.glp.service;

import com.glp.properties.Properties;

public class HelloService {
    
    

    Properties properties;

    public Properties getProperties() {
    
    
        return properties;
    }

    public void setProperties(Properties properties) {
    
    
        this.properties = properties;
    }
    public String sayHello(){
    
    
        return properties.getPre()+"offer"+properties.getSuf();
    }
}

(4)自動構成
自動構成クラスでのサービスと構成クラスの統合は、構成済みクラスをサービスクラスに渡すことと同じです。

package com.glp.config;

import com.glp.properties.Properties;
import com.glp.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(Properties.class)
public class HelloServiceAutoConfiguration {
    
    
    @Autowired
    Properties properties;

    @Bean
    public HelloService getHelloService(){
    
    
        HelloService service = new HelloService();
        service.setProperties(properties);
        return service;
    }
}
  • @ConditionalOnXXXX:特定の条件が満たされているかどうかを判断し、自動構成が有効かどうかを判断するために使用されます
  • @Bean:サービスと構成クラスを統合し、それらをコンポーネントに構成し、ユーザーが呼び出すためにコンテナーに挿入します。
  • @EnableConfigurationProperties :xxxPropertiesを有効にして、コンテナに追加します

(5)春の工場

独自のMETA-INFファイルを作成します。プロジェクトがパッケージのスキャンを開始すると、spring.factoriesファイルで指定された構成クラスが自動的にロードされます。

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.glp.config.HelloServiceAutoConfiguration

2スターターステータスモジュールを作成します

このモジュールは推移的な依存関係にのみ使用されるため、新しいMavenモジュールを作成するだけで十分です。
ここに画像の説明を挿入

pomautoconfigure私たちの習慣を紹介する必要があるだけで、それは依存する役割を果たします。もちろん、したくない場合は自動構成コードと依存関係の管理 これとは別に、1つのモジュールのみを使用することもできます。

    <dependencies>
        <dependency>
            <groupId>com.glp</groupId>
            <artifactId>diy-spring-boot-autoconfiguration</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

注:
自動構成クラス導入するには、自動構成クラスpom.xmlのgroupId、artifactId、およびバージョンをコピーするだけです。
ここに画像の説明を挿入

3自動構成モジュールとスタートアップモジュールをMavenウェアハウスにインストールします

ここに画像の説明を挿入

4テストクラスの作成

新しいSpringBootモジュールテストクラスを作成し、あまり導入せずにWebモジュールをテストクラスに導入しましょう
ここに画像の説明を挿入

(1)テストクラスに依存関係を導入します。

     <dependency>
            <groupId>com.glp</groupId>
            <artifactId>diy-spring-boot-stater</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

(2)Applicantrion.propertiesの構成

diy.hello.pre=where
diy.hello.suf=offer

(3)コントローラーを作成します。

package com.glp.controller;


import com.glp.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    
    @Autowired
    HelloService helloService;

    @GetMapping("/say")
    public String helloController(){
    
    
        return helloService.sayHello();
    }
}

(4)運用結果

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/glpghz/article/details/108427941