Skip to main content
rule CX Codex

spring-performance · kyy-logs, code-review-gate-zh — Claude Code rule for Spring/JPA

spring-performance is a Claude Code rule for Spring/JPA practices and performance tuning. It guides agents working on Java code, database queries, and server settings.

MIT ★ 517 updated 2026-11-16

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/spring-performance.md
Open on GitHub

What spring-performance does

spring-performance sets guidance for request validation, transaction boundaries, exception handling, and JPA queries. To avoid N+1 queries, it suggests JOIN FETCH or EntityGraph; repeated inserts can use a batch operation.

The rule also covers string creation in loops, Linux settings, and JVM heap dumps on OOM. Its Linux recommendations include net.ipv4.tcp_fin_timeout=30 and ulimit -n 65535; the latter sets a suggested file descriptor limit.

What it helps with

  • validate parameters in a Spring controller
  • spot transaction boundaries that need review
  • replace N+1 queries with JOIN FETCH or EntityGraph
  • switch loop-based single inserts to a batch operation
  • reduce repeated string creation while building a result

Example prompts for the agent

  • Review the user creation endpoint against spring-performance.
  • Find JPA queries in this project that cause N+1 loading.
  • Check the user insert loop and suggest a batch operation.

How to install spring-performance

Manually: put the files in place

.claude/rules/spring-performance.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about spring-performance

How do I install spring-performance in Claude Code?

Add the rule at .claude/rules/spring-performance.md.

What does spring-performance do?

It guides Spring/JPA validation, transaction handling, exception handling, query patterns, and performance-related code.

How does spring-performance address N+1 queries?

It recommends JOIN FETCH or EntityGraph to load related orders alongside users.

Source text

spring-performance.md · MIT
# Spring/JPA 与性能优化规范

## Spring/JPA 最佳实践

### 参数验证

```java
// 正确:使用@Valid验证
@PostMapping("/users")
public UserDTO createUser(@Valid @RequestBody UserCreateRequest request) {
    return userService.create(request);
}

@Data
public class UserCreateRequest {
    @NotBlank(message = "用户名不能为空")
    @Size(min = 2, max = 50, message = "用户名长度2-50")
    private String name;
    
    @Email(message = "邮箱格式不正确")
    private String email;
    
    @Min(value = 0, message = "年龄不能为负")
    private Integer age;
}
```

### 事务管理

```java
// 正确:事务范围明确
@Transactional(rollbackFor = Exception.class)
public void createUser(User user, UserProfile profile) {
    userDao.insert(user);
    profile.setUserId(user.getId());
    profileDao.insert(profile);
}

// 注意:只读操作不加事务或使用只读事务
@Transactional(readOnly = true)
public User getUser(Long id) {
    return userDao.findById(id);
}
```

### N+1查询防护

```java
// 错误:N+1查询
List<User> users = userDao.findAll();
for (User user : users) {
    List<Order> orders = orderDao.findByUserId(user.getId());  // N次查询!
    user.setOrders(orders);
}

// 正确:使用JOIN FETCH
@Query("SELECT u FROM User u LEFT JOIN FETCH u.orders WHERE u.id IN :ids")
List<User> findWithOrders(@Param("ids") List<Long> ids);

// 或使用EntityGraph
@Entity
@NamedEntityGraph(name = "User.withOrders", 
    attributeNodes = @NamedAttributeNode("orders"))
public class User { ... }
```

### 统一异常处理

```java
// 统一异常处理
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ErrorResponse> handleBusiness(BusinessException e) {
        return ResponseEntity.badRequest()
            .body(new ErrorResponse(e.getCode(), e.getMessage()));
    }
    
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleOther(Exception e) {
        log.error("Unexpected error", e);
        return ResponseEntity.internalServerError()
            .body(new ErrorResponse("SYSTEM_ERROR", "系统错误"));
    }
}
```

## 性能优化规范

### 服务器配置

| 配置项 | 说明 | 推荐值 |
|-------|------|-------|
| TCP time_wait超时 | 高并发场景调小 | `net.ipv4.tcp_fin_timeout=30` |
| 最大文件句柄数 | Linux默认1024不够 | `ulimit -n 65535` |
| JVM HeapDump | OOM时生成堆转储 | `-XX:+HeapDumpOnOutOfMemoryError` |

### 对象创建

```java
// 错误:循环中创建字符串
String result = "";
for (String item : list) {
    result += item + ",";  // 每次创建新String!
}

// 正确:使用StringBuilder
StringBuilder sb = new StringBuilder();
for (String item : list) {
    if (sb.length() > 0) sb.append(",");
    sb.append(item);
}
String result = sb.toString();

// 或使用String.join
String result = String.join(",", list);
```

### 批量操作

```java
// 错误:循环单条插入
for (User user : users) {
    userDao.insert(user);  // N次IO!
}

// 正确:批量插入
userDao.batchInsert(users);
```

© kyy-logs · MIT · kyy-logs/code-review-gate-skill

Similar IDE rules

All IDE rules for Backend and APIs →

spring-performance by other authors comes first.

spring-performance kyy-logs The spring-performance Claude Code rule sets Spring/JPA and performance checks. It flags issues in validation, transactions, queries, and batch operations. CX Codex +1 ★ 517repo stars java-spring-boot pluginagentmarketplace java-spring-boot generates production-grade Spring Boot 3.x code, covering REST controllers, Spring Security with JWT, Spring Data JPA repositories, and Actuator metrics. CX Codex +1 11,800installs spring-data-jpa giuseppe-trisciuoglio spring-data-jpa is a Claude Code skill for building Spring Data JPA persistence layers. It comes into play for repository, query, relationship, transaction, audit, and pagination work. CX Codex +1 4,300installs spring-boot-crud-patterns giuseppe-trisciuoglio spring-boot-crud-patterns — a skill that builds the whole CRUD layer for Spring Boot 3 services: aggregates, JPA repositories, DTOs and REST controllers in a feature-based layout. CX Codex +1 4,100installs 311-frameworks-spring-jdbc Juan Antonio Breña Moral 311-frameworks-spring-jdbc — a skill that reviews and writes Spring JDBC code: pushes JdbcClient, catches SQL injection, fixes transaction boundaries. CX Codex +1 352installs 312-frameworks-spring-data-jdbc Juan Antonio Breña Moral 312-frameworks-spring-data-jdbc — a skill that applies Spring Data JDBC rules to Java records: entities, repositories, aggregate boundaries, transactions, N+1 avoidance. CX Codex +1 349installs convert-jpa-to-spring-data-cosmos official github convert-jpa-to-spring-data-cosmos — a step-by-step Copilot rule for moving a Spring Boot app off JPA and onto Azure Cosmos DB with Spring Data Cosmos. Useful once the migration is already decided. CX Codex +1 ★ 41,430repo stars

From the same set10

The whole set →
index The index Claude Code rule describes the built-in code review files and how the tool locates them. A project rule pack replaces the built-in pack instead of merging with it. CX Codex +1 ★ 517repo stars baseline-java The baseline-java Claude Code rule adds Java checks for security, concurrency, and exception handling. It applies during Java code review. CX Codex +1 ★ 517repo stars spring-performance The spring-performance Claude Code rule sets Spring/JPA and performance checks. It flags issues in validation, transactions, queries, and batch operations. CX Codex +1 ★ 517repo stars concurrency This Claude Code rule sets Java concurrency guidelines. It covers thread pools, locks, shared records, and other sources of concurrent access bugs. CX Codex +1 ★ 517repo stars exception-logging exception-logging — a rule for Claude Code that enforces Java exception handling and logging standards. Runs during code review to catch empty catch blocks, unmasked data, and missing transaction rollbacks. CX Codex +1 ★ 517repo stars naming-format The naming-format Claude Code rule sets Java naming and formatting conventions. It covers classes, methods, constants, and control flow. CX Codex +1 ★ 517repo stars testing-engineering The testing-engineering Claude Code rule sets expectations for unit tests, project structure, and dependencies. It covers application layers, exception handling, and domain models. CX Codex +1 ★ 517repo stars code-review-gate The code-review-gate Claude Code skill reviews code changes before a commit or PR merge. It gathers the diff, checks test quality, and records issues. CX Codex +1 ★ 517repo stars

Project sponsors

Companies that keep this analytics open