/java-coding-standards
Spring Boot 服务的 Java 编码规范:命名、不可变性、Optional 使用、流(streams)、异常、泛型以及项目布局。
$ npx -y skills add xu-xiang/everything-claude-code-zh --skill java-coding-standards --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/java-coding-standards
Context preview
The summary Claude sees to decide when to auto-load this skill.
Spring Boot 服务的 Java 编码规范:命名、不可变性、Optional 使用、流(streams)、异常、泛型以及项目布局。
SKILL.md
java-coding-standards.SKILL.mdname: java-coding-standards
description: "Spring Boot 服务的 Java 编码规范:命名、不可变性、Optional 使用、流(streams)、异常、泛型以及项目布局。"
origin: ECC
Java 编码规范 (Java Coding Standards)
适用于 Spring Boot 服务中可读、可维护的 Java (17+) 代码规范。
何时激活 (When to Activate)
- 在 Spring Boot 项目中编写或评审 Java 代码时
- 强制执行命名、不可变性(immutability)或异常处理约定时
- 使用 records、密封类(sealed classes)或模式匹配(pattern matching)(Java 17+)时
- 评审 Optional、流(streams)或泛型(generics)的使用时
- 规划包结构和项目布局时
核心原则 (Core Principles)
- 清晰度优先于技巧性
- 默认不可变;尽量减少共享的可变状态
- 快速失败(Fail fast)并提供有意义的异常信息
- 保持命名和包结构的一致性
命名 (Naming)
// ✅ 类/Records:大驼峰(PascalCase)
public class MarketService {}
public record Money(BigDecimal amount, Currency currency) {}
// ✅ 方法/字段:小驼峰(camelCase)
private final MarketRepository marketRepository;
public Market findBySlug(String slug) {}
// ✅ 常量:全大写蛇形(UPPER_SNAKE_CASE)
private static final int MAX_PAGE_SIZE = 100;不可变性 (Immutability)
// ✅ 优先使用 records 和 final 字段
public record MarketDto(Long id, String name, MarketStatus status) {}
public class Market {
private final Long id;
private final String name;
// 仅提供 getter,不提供 setter
}Optional 使用 (Optional Usage)
// ✅ find* 方法应返回 Optional
Optional<Market> market = marketRepository.findBySlug(slug);
// ✅ 使用 Map/flatMap 替代 get()
return market
.map(MarketResponse::from)
.orElseThrow(() -> new EntityNotFoundException("Market not found"));流最佳实践 (Streams Best Practices)
// ✅ 使用流进行转换,保持流水线简短
List<String> names = markets.stream()
.map(Market::name)
.filter(Objects::nonNull)
.toList();
// ❌ 避免复杂的嵌套流;为了清晰起见,优先使用循环异常处理 (Exceptions)
- 对领域错误使用非受检异常(unchecked exceptions);通过上下文包装技术异常
- 创建领域特定的异常(例如 `MarketNotFoundException`)
- 避免捕获广义的 `catch (Exception ex)`,除非是在中心位置重新抛出或记录日志
throw new MarketNotFoundException(slug);
泛型与类型安全 (Generics and Type Safety)
- 避免使用原始类型(raw types);显式声明泛型参数
- 对于可重用的工具类,优先使用有界泛型(bounded generics)
public <T extends Identifiable> Map<Long, T> indexById(Collection<T> items) { ... }项目结构 (Project Structure - Maven/Gradle)
src/main/java/com/example/app/
config/
controller/
service/
repository/
domain/
dto/
util/
src/main/resources/
application.yml
src/test/java/... (与 main 结构镜像)
格式与风格 (Formatting and Style)
- 一致地使用 2 或 4 个空格(遵循项目标准)
- 每个文件仅包含一个公共顶级类型
- 保持方法简短且聚焦;提取助手方法(helpers)
- 成员排序:常量、字段、构造函数、公共方法、受保护方法、私有方法
应避免的代码异味 (Code Smells to Avoid)
- 参数列表过长 → 使用 DTO/构建器(builders)
- 层级嵌套过深 → 提前返回(early returns)
- 魔法数字 → 命名常量
- 静态可变状态 → 优先使用依赖注入(dependency injection)
- 沉默的 catch 块 → 记录日志并处理或重新抛出
日志记录 (Logging)
private static final Logger log = LoggerFactory.getLogger(MarketService.class);
log.info("fetch_market slug={}", slug);
log.error("failed_fetch_market slug={}", slug, ex);空值处理 (Null Handling)
- 仅在不可避免时接受 `@Nullable`;否则使用 `@NonNull`
- 对输入使用 Bean 校验(Bean Validation,如 `@NotNull`, `@NotBlank`)
测试期望 (Testing Expectations)
- 使用 JUnit 5 + AssertJ 进行流式断言
- 使用 Mockito 进行 Mock;尽可能避免部分 Mock(partial mocks)
- 优先使用确定性测试;严禁隐藏的 sleep 等待
**记住**:保持代码有明确意图、强类型且可观测。除非证明确有必要,否则应优先考虑可维护性,而非微优化。
Read more
name: java-coding-standards description: "Spring Boot 服务的 Java 编码规范:命名、不可变性、Optional 使用、流(streams)、异常、泛型以及项目布局。" origin: ECC
Java 编码规范 (Java Coding Standards)
适用于 Spring Boot 服务中可读、可维护的 Java (17+) 代码规范。
何时激活 (When to Activate)
- 在 Spring Boot 项目中编写或评审 Java 代码时
- 强制执行命名、不可变性(immutability)或异常处理约定时
- 使用 records、密封类(sealed classes)或模式匹配(pattern matching)(Java 17+)时
- 评审 Optional、流(streams)或泛型(generics)的使用时
- 规划包结构和项目布局时
核心原则 (Core Principles)
- 清晰度优先于技巧性
- 默认不可变;尽量减少共享的可变状态
- 快速失败(Fail fast)并提供有意义的异常信息
- 保持命名和包结构的一致性
命名 (Naming)
// ✅ 类/Records:大驼峰(PascalCase)
public class MarketService {}
public record Money(BigDecimal amount, Currency currency) {}
// ✅ 方法/字段:小驼峰(camelCase)
private final MarketRepository marketRepository;
public Market findBySlug(String slug) {}
// ✅ 常量:全大写蛇形(UPPER_SNAKE_CASE)
private static final int MAX_PAGE_SIZE = 100;不可变性 (Immutability)
// ✅ 优先使用 records 和 final 字段
public record MarketDto(Long id, String name, MarketStatus status) {}
public class Market {
private final Long id;
private final String name;
// 仅提供 getter,不提供 setter
}Optional 使用 (Optional Usage)
// ✅ find* 方法应返回 Optional
Optional<Market> market = marketRepository.findBySlug(slug);
// ✅ 使用 Map/flatMap 替代 get()
return market
.map(MarketResponse::from)
.orElseThrow(() -> new EntityNotFoundException("Market not found"));流最佳实践 (Streams Best Practices)
// ✅ 使用流进行转换,保持流水线简短
List<String> names = markets.stream()
.map(Market::name)
.filter(Objects::nonNull)
.toList();
// ❌ 避免复杂的嵌套流;为了清晰起见,优先使用循环异常处理 (Exceptions)
- 对领域错误使用非受检异常(unchecked exceptions);通过上下文包装技术异常
- 创建领域特定的异常(例如 `MarketNotFoundException`)
- 避免捕获广义的 `catch (Exception ex)`,除非是在中心位置重新抛出或记录日志
throw new MarketNotFoundException(slug);
泛型与类型安全 (Generics and Type Safety)
- 避免使用原始类型(raw types);显式声明泛型参数
- 对于可重用的工具类,优先使用有界泛型(bounded generics)
public <T extends Identifiable> Map<Long, T> indexById(Collection<T> items) { ... }项目结构 (Project Structure - Maven/Gradle)
src/main/java/com/example/app/ config/ controller/ service/ repository/ domain/ dto/ util/ src/main/resources/ application.yml src/test/java/... (与 main 结构镜像)
格式与风格 (Formatting and Style)
- 一致地使用 2 或 4 个空格(遵循项目标准)
- 每个文件仅包含一个公共顶级类型
- 保持方法简短且聚焦;提取助手方法(helpers)
- 成员排序:常量、字段、构造函数、公共方法、受保护方法、私有方法
应避免的代码异味 (Code Smells to Avoid)
- 参数列表过长 → 使用 DTO/构建器(builders)
- 层级嵌套过深 → 提前返回(early returns)
- 魔法数字 → 命名常量
- 静态可变状态 → 优先使用依赖注入(dependency injection)
- 沉默的 catch 块 → 记录日志并处理或重新抛出
日志记录 (Logging)
private static final Logger log = LoggerFactory.getLogger(MarketService.class);
log.info("fetch_market slug={}", slug);
log.error("failed_fetch_market slug={}", slug, ex);空值处理 (Null Handling)
- 仅在不可避免时接受 `@Nullable`;否则使用 `@NonNull`
- 对输入使用 Bean 校验(Bean Validation,如 `@NotNull`, `@NotBlank`)
测试期望 (Testing Expectations)
- 使用 JUnit 5 + AssertJ 进行流式断言
- 使用 Mockito 进行 Mock;尽可能避免部分 Mock(partial mocks)
- 优先使用确定性测试;严禁隐藏的 sleep 等待
**记住**:保持代码有明确意图、强类型且可观测。除非证明确有必要,否则应优先考虑可维护性,而非微优化。
🌐 Language / 语言 / 語言 为 AI 智能体(Agent)框架打造的性能优化系统。源自 Anthropic 黑客松获胜作品。 这不仅仅是配置文件。它是一个完整的系统:包含技能(Skills)、本能(Instincts)、内存优化、持续学习、安全扫描以及研究优先的开发模式。这些生产级的智能体(Agents)、钩子(Hooks)、命令(Commands)、规则(Rules)以及 MCP 配置,是在构建真实产品的 10 个多月高强度日常使用中演化而来的。 适用于 Claude Code, Codex,
Repo: xu-xiang/everything-claude-code-zh
Other skills on everything-claude-code.
- /oneskill
发现技能(Skill),迭代查询,并在任何环境中自动安装技能。
Open skill - /api-design
生产级 API 的 REST API 设计模式,包括资源命名、状态码、分页、过滤、错误响应、版本控制和速率限制。
Open skill - /article-writing
编写文章、指南、博客帖子、教程、新闻通讯(newsletter)以及其他长篇内容。这些内容具有从提供的示例或品牌指南中提取出的独特语气。当用户需要比段落更长的精美文案,且对语气一致性、结构和可信度有要求时,请使用此技能(Skill)。
Open skill - /autonomous-loops
自主运行 Claude Code 循环的模式与架构 —— 从简单的顺序流水线到 RFC 驱动的多智能体 DAG 系统。
Open skill - /backend-patterns
后端架构模式、API 设计、数据库优化以及针对 Node.js、Express 和 Next.js API 路由的服务端最佳实践。
Open skill - /clickhouse-io
ClickHouse 数据库模式、查询优化、分析以及高性能分析负载的数据工程最佳实践。
Open skill

