[ケース戦闘] SpringBoot3.x カスタムパッケージ スターター戦闘

1.スターターの背景紹介と機能

(1) スターターとは

starter は SpringBoot の新しい発明であり、プロジェクト開発プロセスの複雑さを効果的に軽減し、開発操作の簡素化に非常に優れた効果をもたらします。

スターターの概念: スターターには使用されるすべての依存関係が含まれ、開発者自身が依存関係を導入することによって引き起こされるトラブルを回避します。異なる依存関係を解決するために異なるスターターが使用されるため、内部実装が大きく異なる可能性があることに注意してください。たとえば、スターターの本質は合成にあるため、jpa のスターターと Redis のスターターは異なる実装を持つ可能性があります。 , これは、論理レベルでの抽象化の層です。おそらく、この概念は Docker にいくらか似ています。なぜなら、これらはすべて「パッケージ化」操作を実行しているからです。Docker がどのような問題を解決するように設計されているかがわかっている場合は、Docker Make an を使用できるかもしれません。スターターとの類似。

(2) スターターの背景をカスタマイズする

  • 誰もがStarterをたくさん使ったことがあるはずで、ミドルウェアもたくさんありますが、Starterがなかったり、互換性がないミドルウェアもたくさんあります。
  • たとえば、Spring Boot 3.x バージョンが更新された場合、一部の Starter は更新する時間がないため、使用できません。
  • 実際の企業の技術チームのリーダーまたはアーキテクトも、
  • 独自のプロジェクト グループのスターターをカプセル化して開発プロジェクトを高速化し、統合および標準化して構成を簡素化できます。

(3) スターターパッケージ仕様のカスタマイズ

  • 公式スターターパッケージ仕様: spring-boot-starter-xxx
  • カスタムスターターパッケージ仕様: xxx-spring-boot-starter
spring-boot-starter
spring-boot-starter-data-jpa
spring-boot-starter-data-redis
spring-boot-starter-data-mongodb
spring-boot-starter-jdbc
mybatis-spring-boot-starter
mybatis-plus-boot-starter

(4) 新バージョン Spring Boot3.X と旧バージョン SpringBoot2.7 以前のカスタム Starter の違い

  • SpringBoot2.7以前

    • META-INF/spring.factories ファイルを追加します。org.springframework.boot.autoconfigure.EnableAutoConfiguration=XXAutoConfiguration
    • springboot2.7以前のカスタムスターターについては別記事で紹介します。ここは後ほど更新します!

    ここに画像の説明を挿入

  • SpringBoot2.7 では新しい自動構成が導入されています

    • META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports にあります。
    • 構成クラスの名前をファイルに追加します。各行には構成クラスの完全修飾名が含まれます。
    • META-INF/spring.factories メソッドと互換性あり
  • SpringBoot3.x は spring.factories を削除します

    • META-INF/spring.factories 方法を削除する
    • META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports のみをサポート 自動構成の追加

2. スターターをカスタマイズする手順

  • プロジェクト xx-spring-boot-starter を作成する

  • 依存関係を追加する

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>3.0.2</version>
</dependency>

  • 構成クラスを作成する
  • XXAutoConfiguration クラスを作成する
  • Condition 条件の注釈を追加する
  • AutoConfiguration.imports 自動構成クラスを構成する

3. スターターケースを実際にカスタマイズする

(1) 需要の背景

  • 現在、同社の SMS 送信はすべてメッセージ センターに統合されており、各プラットフォームの開発を簡素化するために、SMS スターターがカスタマイズされ、使用するプラットフォームごとにパッケージ化されています。
  • テキスト メッセージを送信するための sms スターターをカスタマイズします。スターターは、Alibaba Cloud、Tencent Cloud、Amazon Cloud などのさまざまなサードパーティ SMS プラットフォームに接続されます。開発者は、構成に従って SMS プロバイダーを構成できます。デフォルトのプロバイダーは Alibaba です。雲。
  • Spring コンテナに対応する Bean がない場合は作成され、存在する場合は作成されません。

(2) SpringBoot3.x プロジェクト sms-spring-boot-starter を作成し、autoconfigure の依存関係を追加します

ここに画像の説明を挿入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>3.0.2</version>
        </dependency>

(3) 構成クラスを作成し、@ConfigurationProperties アノテーションを使用して application.yml 構成ファイルをバインドします。

  • SMS オペレーターの型列挙クラスを作成する
public enum SmsTypeEnum {
    
    
		//阿里云
    ALI_CLOUD("ali"),
  	//腾讯云
    TX_CLOUD("tx"),
  	//亚马逊云
    YMX_CLOUD("ymx");

    private String type;

    SmsTypeEnum(String ymx) {
    
    }

}
@ConfigurationProperties(prefix = "sms.server.achieve")
public class SmsProperties {
    
    
		
  	/**
   	 * 发送短信类型
   	 */
    private String type;

    public String getType() {
    
    
        if(type == null || "".equals(type)){
    
    
            type = SmsTypeEnum.ALI_YUN.name();
        }
        return type;
    }

    public void setType(String type) {
    
    
        this.type = type;
    }
}

(4) SmsService インターフェイスを作成し、3 つのクラウド ベンダーの SMS 送信の実装をカプセル化します。

public interface SmsService {
    
    
    String send(String fromPhone,String toPhone,String content);
}
/**
 * 阿里云SMS实现
 * @author lixiang
 * @date 2023/5/16 09:30
 */
@Service("ali")
public class AliCloudSmsServiceImpl implements SmsService {
    
    

    @Override
    public String send(String fromPhone, String toPhone, String content) {
    
    
        System.out.println("------------------当前SMS厂商为阿里云------------------");
        System.out.println("----"+fromPhone+" 向 "+toPhone +" 发送了一条短信。"+"----");
        System.out.println("短信内容为:"+content);
        System.out.println("----------------------------------------------------");
        return "success";
    }
}
/**
 * 腾讯云SMS实现
 * @author lixiang
 * @date 2023/5/16 09:44
 */
@Service("tx")
public class TxCloudSmsServiceImpl implements SmsService {
    
    
    @Override
    public String send(String fromPhone, String toPhone, String content) {
    
    
        System.out.println("------------------当前SMS厂商为腾讯云------------------");
        System.out.println("----"+fromPhone+" 向 "+toPhone +" 发送了一条短信。"+"----");
        System.out.println("短信内容为:"+content);
        System.out.println("----------------------------------------------------");
        return "success";
    }
}
/**
 * 亚马逊云SMS实现
 * @author lixiang
 * @date 2023/5/16 09:42
 */
@Service("ymx")
public class YmxCloudSmsServiceImpl implements SmsService {
    
    
    @Override
    public String send(String fromPhone, String toPhone, String content) {
    
    
        System.out.println("------------------当前SMS厂商为亚马逊云------------------");
        System.out.println("----"+fromPhone+" 向 "+toPhone +" 发送了一条短信。"+"----");
        System.out.println("短信内容为:"+content);
        System.out.println("----------------------------------------------------");
        return "success";
    }
}

(5) 統一したサービス提供のためのSmsTemplateの定義

/**
 * @author lixiang
 * @date 2023/5/16 09:33
 */
public class SmsTemplate {
    
    

    @Autowired
    private SmsProperties smsProperties;

    @Autowired
    private ApplicationContext context;

    public String send(String fromPhone,String toPhone,String content){
    
    
        //获取云厂商的业务实现类
        String type = smsProperties.getType();
        SmsService smsService = (SmsService)context.getBean(type);
        return smsService.send(fromPhone,toPhone,content);
    }
}

(6) SmsAutoConfigurationクラスを定義する

/**
 * @author lixiang
 * @date 2023/5/16 09:27
 */
@AutoConfiguration
@ConditionalOnClass(SmsTemplate.class)
@EnableConfigurationProperties(value = SmsProperties.class)
public class SmsAutoConfiguration {
    
    

    @Bean
    @ConditionalOnMissingBean
    public SmsTemplate smsTemplate(){
    
    
        return new SmsTemplate();
    }

}

(7) インポート構成ファイルを作成し、自動的にロードする必要があるクラスを構成します。

  • @AutoConfiguration は Spring Boot 2.7 で新しく導入されました。自動構成クラスと見なされるには、自動構成クラスを次のファイルに配置する必要があります
  • META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

ここに画像の説明を挿入

com.lixiang.config.SmsAutoConfiguration

(8) ローカルウェアハウスへの mvn クリーンインストール

ここに画像の説明を挿入

4. カスタム スターターを確認する

(1) 新規プロジェクトを作成する場合は、springboot3.x 以降のバージョンに注意してください。

        <dependency>
            <groupId>com.lixiang</groupId>
            <artifactId>sms-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

(2) テストコードの作成

@RestController
@RequestMapping("/spring-test")
public class TestController {
    
    

    @Autowired
    private SmsTemplate smsTemplate;

    @RequestMapping("/sms")
    public String sms(){
    
    
        String fromPhone = "15522834580";
        String toPhone = "13820345839";
        String content = "李祥,李祥,今晚王者峡谷 六点 五缺一,收到请回复,over!";
        return smsTemplate.send(fromPhone,toPhone,content);
    }

}

(3) 設定ファイルが設定されていない場合、デフォルトで Alibaba Cloud のベンダーが使用されます。

ここに画像の説明を挿入

(4) クラウドベンダーが設定されている場合、設定されているクラウドベンダーが使用されます。

ここに画像の説明を挿入
ここに画像の説明を挿入

さて、SpringBoot3.x のカスタム スターターが完成しました。現在、多くの企業がまだ springboot2.x バージョンを使用しているため、springboot2.x のカスタム スターターの手順は後で更新されます。

おすすめ

転載: blog.csdn.net/weixin_47533244/article/details/130703512