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. 04通过 DTO record 定义 REST 控制器

适用场景

  1. 01构建 Spring 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:类型化配置属性

使用带验证的 @ConfigurationProperties,而不是零散使用 @Value。

模式 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 record 的 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

资源

评论

支持 Markdown