spring-boot-3 について
Spring Boot 3.3以降でサービスやAPIを開発する人向けのスキルです。依存性注入、設定の検証、サービス層のトランザクション、RESTコントローラーの実装パターンを示します。
主な機能
- 01コンストラクターインジェクションの使用
- 02型付き設定プロパティの検証
- 03サービス層でのトランザクション境界
- 04DTO recordによるRESTコントローラー定義
活用シーン
- 01Spring BootサービスやAPIの構築
- 02アプリケーション設定の定義と検証
- 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 |
リソース
- Spring Boot リファレンス:https://docs.spring.io/spring-boot/docs/current/reference/html/
- Spring Framework リファレンス:https://docs.spring.io/spring-framework/reference/
コメント
Markdown 対応