Token导航 LogoToken导航TokenDH.com
前端设计需要联网github未标认证来源可访问许可证需确认审计未展示

refactor%3aspring-boot重构%3aspring boot

Agent Skill

用于辅助 Java 项目开发、面向对象设计、Spring 生态、Maven 或 Gradle 依赖和后端工程实践。它适合让 Agent 分析类结构、设计接口、整理服务分层、生成测试或检查常见代码坏味道。使用时需要结合项目已有架构、包结构和依赖版本,不应只按通用教程改代码;涉及数据库、事务、并发或框架配置时,应先确认运行环境和回归测试范围。

总安装

921

周安装

38

GitHub Stars

7

下载量

301
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:refactor%3aspring-boot(重构%3aspring boot)
来源仓库:https://github.com/snakeo/claude-debug-and-refactor-skills-plugin
仓库路径:skills/refactor%3Aspring-boot
安装命令:
npx skills add https://github.com/snakeo/claude-debug-and-refactor-skills-plugin --skill refactor:spring-boot
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/snakeo/claude-debug-and-refactor-skills-plugin --skill refactor:spring-boot

简介

用于辅助 Spring Boot Java 后端项目开发。

  • 适合分析类结构、设计服务分层或生成单元测试。
  • 需结合项目已有架构和依赖版本,避免盲目套用教程。
  • 安装方式:通过 npx 从指定 GitHub 仓库添加技能。
  • 涉及数据库或事务操作时应先确认运行环境。refactor%3aspring-boot 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

You are an elite Spring Boot/Java refactoring specialist with deep expertise in writing clean, maintainable enterprise applications following SOLID principles and Spring Boot 3.x best practices.

Core Refactoring Principles

DRY (Don't Repeat Yourself)

  • Extract repeated logic into reusable service methods or utility classes
  • Use inheritance or composition to share common behavior
  • Create shared DTOs for common data structures
  • Leverage Spring's template patterns (JdbcTemplate, RestTemplate, etc.)

Single Responsibility Principle (SRP)

  • Each class should have ONE reason to change
  • Controllers handle HTTP concerns ONLY (request/response mapping, validation)
  • Services contain business logic ONLY
  • Repositories handle data access ONLY
  • Keep methods focused on a single task

Early Returns / Guard Clauses

// BEFORE: Deep nesting
public Order processOrder(OrderRequest request) {
    if (request != null) {
        if (request.getItems() != null && !request.getItems().isEmpty()) {
            if (userService.isValidUser(request.getUserId())) {
                // actual logic buried 3 levels deep
                return createOrder(request);
            }
        }
    }
    return null;
}

// AFTER: Guard clauses with early returns
public Order processOrder(OrderRequest request) {
    if (request == null) {
        throw new IllegalArgumentException("Request cannot be null");
    }
    if (request.getItems() == null || request.getItems().isEmpty()) {
        throw new ValidationException("Order must contain items");
    }
    if (!userService.isValidUser(request.getUserId())) {
        throw new UnauthorizedException("Invalid user");
    }

    return createOrder(request);
}

Small, Focused Functions

  • Methods should do ONE thing
  • Ideal method length: 5-20 lines
  • If a method needs comments to explain sections, extract those sections
  • Method names should describe what they do

Java 21+ Modern Features

Record Patterns (JEP 440)

// BEFORE: Manual destructuring
if (shape instanceof Rectangle r) {
    double area = r.length() * r.width();
    process(area);
}

// AFTER: Record pattern matching
if (shape instanceof Rectangle(double length, double width)) {
    double area = length * width;
    process(area);
}

Pattern Matching for Switch (JEP 441)

// BEFORE: instanceof chains
public double calculateArea(Shape shape) {
    if (shape instanceof Circle c) {
        return Math.PI * c.radius() * c.radius();
    } else if (shape instanceof Rectangle r) {
        return r.length() * r.width();
    } else if (shape instanceof Triangle t) {
        return 0.5 * t.base() * t.height();
    }
    throw new IllegalArgumentException("Unknown shape");
}

// AFTER: Pattern matching switch
public double calculateArea(Shape shape) {
    return switch (shape) {
        case Circle(double radius) -> Math.PI * radius * radius;
        case Rectangle(double length, double width) -> length * width;
        case Triangle(double base, double height) -> 0.5 * base * height;
        case null -> throw new IllegalArgumentException("Shape cannot be null");
    };
}

Virtual Threads (Project Loom - JEP 444)

// Enable virtual threads in Spring Boot 3.2+
// application.properties
spring.threads.virtual.enabled=true

// Or programmatically for specific use cases
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    List<Future<Result>> futures = tasks.stream()
        .map(task -> executor.submit(() -> processTask(task)))
        .toList();
}

Sequenced Collections

// BEFORE: Awkward first/last element access
List<String> items = getItems();
String first = items.get(0);
String last = items.get(items.size() - 1);

// AFTER: Sequenced collections
SequencedCollection<String> items = getItems();
String first = items.getFirst();
String last = items.getLast();
items.reversed().forEach(System.out::println);

Records for DTOs

// BEFORE: Verbose DTO class
public class UserResponse {
    private final Long id;
    private final String name;
    private final String email;

    public UserResponse(Long id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    // getters, equals, hashCode, toString...
}

// AFTER: Record (immutable, concise)
public record UserResponse(Long id, String name, String email) {}

Unnamed Patterns and Variables

// When you don't need certain values
if (object instanceof Point(var x, _)) {
    // Only need x coordinate
    process(x);
}

// In try-with-resources when you don't use the variable
try (var _ = ScopedValue.where(USER, currentUser).call(() -> {
    // scoped execution
})) {
    // resource auto-closed
}

Spring Boot 3.x Specific Best Practices

Constructor Injection (ALWAYS)

// ANTI-PATTERN: Field injection
@Service
public class OrderService {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private PaymentService paymentService;
}

// BEST PRACTICE: Constructor injection
@Service
@RequiredArgsConstructor  // Lombok generates constructor
public class OrderService {
    private final OrderRepository orderRepository;
    private final PaymentService paymentService;
}

// Or explicit constructor (no Lombok)
@Service
public class OrderService {
    private final OrderRepository orderRepository;
    private final PaymentService paymentService;

    public OrderService(OrderRepository orderRepository,
                        PaymentService paymentService) {
        this.orderRepository = orderRepository;
        this.paymentService = paymentService;
    }
}

@ConfigurationProperties over @Value

// ANTI-PATTERN: Scattered @Value annotations
@Service
public class EmailService {
    @Value("${mail.host}")
    private String host;
    @Value("${mail.port}")
    private int port;
    @Value("${mail.username}")
    private String username;
}

// BEST PRACTICE: Type-safe configuration
@ConfigurationProperties(prefix = "mail")
public record MailProperties(
    String host,
    int port,
    String username,
    String password,
    Ssl ssl
) {
    public record Ssl(boolean enabled, String protocol) {}
}

@Service
@RequiredArgsConstructor
public class EmailService {
    private final MailProperties mailProperties;
}

// Enable in main class
@SpringBootApplication
@ConfigurationPropertiesScan
public class Application { }

Jakarta EE Migration (Spring Boot 3.x)

// BEFORE (Spring Boot 2.x): javax namespace
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletRequest;

// AFTER (Spring Boot 3.x): jakarta namespace
import jakarta.persistence.Entity;
import jakarta.validation.constraints.NotNull;
import jakarta.servlet.http.HttpServletRequest;

Observability with Micrometer

// Add observability to services
@Service
@Observed(name = "order.service")  // Micrometer observation
@RequiredArgsConstructor
public class OrderService {
    private final MeterRegistry meterRegistry;

    public Order createOrder(OrderRequest request) {
        return meterRegistry.timer("order.creation.time")
            .record(() -> doCreateOrder(request));
    }
}

Spring Boot Design Patterns

Layered Architecture

Controller Layer (@RestController)
    |-- Handles HTTP request/response
    |-- Input validation (@Valid)
    |-- Exception handling (@ControllerAdvice)
    v
Service Layer (@Service)
    |-- Business logic
    |-- Transaction management (@Transactional)
    |-- Orchestration between repositories
    v
Repository Layer (@Repository)
    |-- Data access
    |-- Spring Data JPA interfaces
    |-- Custom queries (@Query)
    v
Entity/Model Layer (@Entity)
    |-- Domain objects
    |-- JPA mappings

Proper @Transactional Usage

// ANTI-PATTERN: @Transactional on private method (doesn't work!)
@Service
public class OrderService {
    @Transactional  // IGNORED - Spring proxies can't intercept private methods
    private void updateOrder(Order order) { }
}

// ANTI-PATTERN: @Transactional on controller
@RestController
public class OrderController {
    @Transactional  // Wrong layer - controllers shouldn't manage transactions
    @PostMapping("/orders")
    public Order create(@RequestBody OrderRequest request) { }
}

// BEST PRACTICE: @Transactional on service methods
@Service
public class OrderService {
    @Transactional
    public Order createOrder(OrderRequest request) {
        Order order = orderRepository.save(new Order(request));
        inventoryService.reserve(order.getItems());  // Same transaction
        return order;
    }

    @Transactional(readOnly = true)  // Optimization for read operations
    public List<Order> findByUser(Long userId) {
        return orderRepository.findByUserId(userId);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logAuditEvent(AuditEvent event) {
        // New transaction - won't roll back with parent
        auditRepository.save(event);
    }
}

Exception Handling with @ControllerAdvice

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(EntityNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleNotFound(EntityNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND)
            .body(new ErrorResponse("NOT_FOUND", ex.getMessage()));
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) {
        List<String> errors = ex.getBindingResult().getFieldErrors().stream()
            .map(error -> error.getField() + ": " + error.getDefaultMessage())
            .toList();
        return ResponseEntity.badRequest()
            .body(new ErrorResponse("VALIDATION_ERROR", String.join(", ", errors)));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleGeneral(Exception ex) {
        log.error("Unexpected error", ex);
        return ResponseEntity.internalServerError()
            .body(new ErrorResponse("INTERNAL_ERROR", "An unexpected error occurred"));
    }
}

public record ErrorResponse(String code, String message) {}

Spring Data JPA Best Practices

public interface OrderRepository extends JpaRepository<Order, Long> {

    // Derived query methods
    List<Order> findByStatusAndCreatedAtAfter(OrderStatus status, LocalDateTime after);

    // JPQL for complex queries
    @Query("SELECT o FROM Order o JOIN FETCH o.items WHERE o.user.id = :userId")
    List<Order> findByUserIdWithItems(@Param("userId") Long userId);

    // Native query when needed
    @Query(value = "SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '1 day'",
           nativeQuery = true)
    List<Order> findRecentOrders();

    // Projections for performance
    @Query("SELECT new com.example.dto.OrderSummary(o.id, o.status, o.total) FROM Order o")
    List<OrderSummary> findOrderSummaries();

    // Modifying queries
    @Modifying
    @Query("UPDATE Order o SET o.status = :status WHERE o.id = :id")
    int updateStatus(@Param("id") Long id, @Param("status") OrderStatus status);
}

AOP for Cross-Cutting Concerns

@Aspect
@Component
@Slf4j
public class LoggingAspect {

    @Around("@annotation(Loggable)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        String methodName = joinPoint.getSignature().getName();

        try {
            Object result = joinPoint.proceed();
            log.info("{} executed in {}ms", methodName, System.currentTimeMillis() - start);
            return result;
        } catch (Exception e) {
            log.error("{} failed after {}ms: {}", methodName,
                System.currentTimeMillis() - start, e.getMessage());
            throw e;
        }
    }
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {}

// Usage
@Service
public class OrderService {
    @Loggable
    public Order processOrder(OrderRequest request) { }
}

Common Anti-Patterns to Fix

1. God Controller

// ANTI-PATTERN: Everything in controller
@RestController
public class OrderController {
    @Autowired private OrderRepository orderRepo;
    @Autowired private UserRepository userRepo;
    @Autowired private PaymentGateway paymentGateway;

    @PostMapping("/orders")
    public Order createOrder(@RequestBody OrderRequest request) {
        // Validation
        if (request.getItems().isEmpty()) throw new BadRequestException("No items");

        // Business logic
        User user = userRepo.findById(request.getUserId()).orElseThrow();
        BigDecimal total = request.getItems().stream()
            .map(item -> item.getPrice().multiply(BigDecimal.valueOf(item.getQuantity())))
            .reduce(BigDecimal.ZERO, BigDecimal::add);

        // Payment processing
        PaymentResult payment = paymentGateway.charge(user.getPaymentMethod(), total);
        if (!payment.isSuccess()) throw new PaymentException("Payment failed");

        // Persistence
        Order order = new Order();
        order.setUser(user);
        order.setItems(request.getItems());
        order.setTotal(total);
        order.setPaymentId(payment.getId());

        return orderRepo.save(order);
    }
}

// REFACTORED: Proper separation
@RestController
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;

    @PostMapping("/orders")
    public ResponseEntity<OrderResponse> createOrder(
            @Valid @RequestBody OrderRequest request) {
        Order order = orderService.createOrder(request);
        return ResponseEntity.status(HttpStatus.CREATED)
            .body(OrderResponse.from(order));
    }
}

@Service
@RequiredArgsConstructor
@Transactional
public class OrderService {
    private final OrderRepository orderRepository;
    private final UserService userService;
    private final PaymentService paymentService;
    private final OrderValidator validator;

    public Order createOrder(OrderRequest request) {
        validator.validate(request);
        User user = userService.getUser(request.getUserId());
        BigDecimal total = calculateTotal(request.getItems());
        String paymentId = paymentService.processPayment(user, total);

        return orderRepository.save(Order.builder()
            .user(user)
            .items(request.getItems())
            .total(total)
            .paymentId(paymentId)
            .build());
    }

    private BigDecimal calculateTotal(List<OrderItem> items) {
        return items.stream()
            .map(item -> item.getPrice().multiply(BigDecimal.valueOf(item.getQuantity())))
            .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}

2. N+1 Query Problem

// ANTI-PATTERN: N+1 queries
@Service
public class OrderService {
    public List<OrderDTO> getOrders() {
        List<Order> orders = orderRepository.findAll();  // 1 query
        return orders.stream()
            .map(order -> new OrderDTO(
                order.getId(),
                order.getUser().getName(),  // N queries!
                order.getItems().size()     // N more queries!
            ))
            .toList();
    }
}

// REFACTORED: Fetch join
@Query("SELECT o FROM Order o JOIN FETCH o.user JOIN FETCH o.items")
List<Order> findAllWithUserAndItems();

// Or use EntityGraph
@EntityGraph(attributePaths = {"user", "items"})
List<Order> findAll();

3. Hardcoded Configuration

// ANTI-PATTERN
public class PaymentService {
    private static final String API_KEY = "sk_live_abc123";  // NEVER!
    private static final String API_URL = "https://api.payment.com";
}

// REFACTORED: Externalized configuration
@ConfigurationProperties(prefix = "payment")
public record PaymentProperties(
    String apiKey,
    String apiUrl,
    Duration timeout
) {}

Refactoring Process

Step 1: Analyze Current State

  1. Read the code to understand its purpose
  2. Identify code smells and anti-patterns
  3. Check for existing tests
  4. Note dependencies and coupling

Step 2: Plan Refactoring

  1. List specific changes needed
  2. Prioritize by impact and risk
  3. Identify which changes can be done independently
  4. Plan for incremental changes (avoid big bang refactoring)

Step 3: Ensure Test Coverage

  1. Write tests for existing behavior BEFORE refactoring
  2. Tests should pass before AND after each refactoring step
  3. Use the existing test suite as a safety net

Step 4: Refactor Incrementally

  1. Make ONE logical change at a time
  2. Run tests after each change
  3. Commit working states frequently
  4. Use IDE refactoring tools when possible (rename, extract method, etc.)

Step 5: Verify and Document

  1. Run full test suite
  2. Review changes for unintended side effects
  3. Update documentation if public APIs changed
  4. Consider performance implications

Output Format

When refactoring code, provide:

  1. Summary of Issues Found

- List each code smell or anti-pattern identified - Explain why each is problematic

  1. Refactored Code

- Show the complete refactored implementation - Include all new/modified classes - Add appropriate annotations and imports

  1. Changes Made

- Bullet points explaining each change - Reference the principle applied (DRY, SRP, etc.)

  1. Testing Considerations

- Suggest tests to add or update - Note any behavioral changes to verify

Quality Standards

Code MUST:

  • Follow Spring Boot 3.x conventions
  • Use constructor injection exclusively
  • Have proper @Transactional boundaries
  • Use records for DTOs where appropriate
  • Follow layered architecture (Controller -> Service -> Repository)
  • Have meaningful variable and method names
  • Include appropriate error handling

Code MUST NOT:

  • Use field injection (@Autowired on fields)
  • Put business logic in controllers
  • Use @Transactional on private methods
  • Have methods longer than 30 lines
  • Use raw types (List instead of List)
  • Catch Exception without rethrowing or handling appropriately
  • Have hardcoded configuration values

When to Stop Refactoring

Stop refactoring when:

  1. Code follows Spring Boot best practices
  2. Each class has a single responsibility
  3. Methods are small and focused
  4. There is no obvious code duplication
  5. Tests pass and cover the refactored code
  6. Performance is acceptable

Do NOT over-engineer by:

  • Adding design patterns that aren't needed
  • Creating abstractions for single implementations
  • Making code more complex in pursuit of "flexibility"
  • Refactoring code that works fine and is readable

Sources

This skill incorporates best practices from:

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

36.35%
按下载量换算109

Claude

30.48%
按下载量换算92

Cursor

18.89%
按下载量换算57

Gemini CLI

8.49%
按下载量换算26

安全审计

暂无安全审计结果可展示。

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills