mcpField
S
spring-boot-3

spring-boot-3

github.com/gentleman-programming開発661142 回インストール31 週間前に更新
リポジトリ

spring-boot-3 について

Spring Boot 3.3以降でサービスやAPIを開発する人向けのスキルです。依存性注入、設定の検証、サービス層のトランザクション、RESTコントローラーの実装パターンを示します。

主な機能

  1. 01コンストラクターインジェクションの使用
  2. 02型付き設定プロパティの検証
  3. 03サービス層でのトランザクション境界
  4. 04DTO recordによるRESTコントローラー定義

活用シーン

  1. 01Spring BootサービスやAPIの構築
  2. 02アプリケーション設定の定義と検証
  3. 03トランザクション対応サービスとRESTコントローラーの実装

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/gentleman-programming/gentleman-skills --skill spring-boot-3

インストール後、Claude Code で/spring-boot-3を入力して読み込まれたか確認します。

詳細

使用するタイミング

次の場合に、このスキルを読み込みます。

  • Spring Boot 3.3+ サービスまたは API を構築する場合
  • 依存性注入で Bean を配線する場合
  • 設定プロパティとバリデーションを定義する場合
  • REST コントローラーとサービスレイヤーを実装する場合

重要なパターン

パターン 1:コンストラクター注入のみ

常にコンストラクター注入を使用し、フィールド注入は避けます。

パターン 2:型安全な設定プロパティ

散在する @Value ではなく、バリデーションと併せて @ConfigurationProperties を使用します。

パターン 3:サービスレイヤーにおけるトランザクション境界

コントローラーではなく、アプリケーションサービスに @Transactional を適用します。

コード例

例 1:バリデーション付き設定プロパティ
package com.acme.config;

import jakarta.validation.constraints.NotBlank;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

@Validated
@ConfigurationProperties(prefix = "payment")
public record PaymentProperties(
  @NotBlank String provider,
  @NotBlank String apiKey
) { }
package com.acme;

import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@ConfigurationPropertiesScan
public class Application { }
例 2:コンストラクター注入とトランザクションを使ったサービス
package com.acme.order.application;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public final class OrderService {
  private final OrderRepository repository;

  public OrderService(OrderRepository repository) {
    this.repository = repository;
  }

  @Transactional
  public void placeOrder(OrderCommand command) {
    repository.save(command.toEntity());
  }
}
例 3:DTO レコードを使った REST コントローラー
package com.acme.order.api;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public final class OrderController {
  private final OrderService service;

  public OrderController(OrderService service) {
    this.service = service;
  }

  @PostMapping
  public ResponseEntity<OrderResponse> place(@RequestBody OrderRequest request) {
    service.placeOrder(request.toCommand());
    return ResponseEntity.ok(new OrderResponse("ok"));
  }

  public record OrderRequest(String sku, int qty) {
    public OrderCommand toCommand() { return new OrderCommand(sku, qty); }
  }

  public record OrderResponse(String status) { }
}

アンチパターン

使用しない:フィールド注入
// BAD: field injection
@Service
public class OrderService {
  @org.springframework.beans.factory.annotation.Autowired
  private OrderRepository repository;
}
使用しない:@Value による設定の散在
// BAD: hard to validate and test
@Service
public class PaymentService {
  @org.springframework.beans.factory.annotation.Value("${payment.apiKey}")
  private String apiKey;
}

クイックリファレンス

タスクパターン
依存関係を注入するコンストラクター注入のみ
設定を読み込む@ConfigurationProperties + @Validated
トランザクションサービスに @Transactional

リソース

コメント