Java Coding Standards logo

Java Coding Standards

CommunityPopular
xu-xiang
java-coding-standards

适用于 Spring Boot 服务的 Java 编码规范:命名、不可变性、Optional 使用、流(Stream)、异常、泛型及项目布局。

Overview

Publisherxu-xiang
Repositoryeverything-claude-code-zh
Skill namejava-coding-standards
Stars
1.9K
Forks
318
Bundled files
Instructions only
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by xu-xiang on GitHub. Read the source before you install it.

Installation

Install the Java Coding Standards AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git /tmp/everything-claude-code-zh
mkdir -p .claude/skills
cp -r /tmp/everything-claude-code-zh/docs/ja-JP/skills/java-coding-standards .claude/skills/java-coding-standards
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Java Coding Standards in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Java Coding Standards on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Java Coding Standards is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Java 编码规范

适用于 Spring Boot 服务中易读且可维护的 Java (17+) 代码标准。

核心原则

  • 清晰度优先于巧妙性
  • 默认不可变;最小化共享的可变状态
  • 抛出有意义的异常以实现早期失败(Fail fast)
  • 一致的命名与包结构

命名

java
// ✅ 类/记录(Record): 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;

不可变性

java
// ✅ 优先使用 record 和 final 字段
public record MarketDto(Long id, String name, MarketStatus status) {}

public class Market {
  private final Long id;
  private final String name;
  // 仅有 getter,没有 setter
}

Optional 的使用

java
// ✅ find* 方法返回 Optional
Optional<Market> market = marketRepository.findBySlug(slug);

// ✅ 使用 map/flatMap 代替 get()
return market
    .map(MarketResponse::from)
    .orElseThrow(() -> new EntityNotFoundException("Market not found"));

流(Stream)最佳实践

java
// ✅ 使用流进行转换,保持流水线(Pipeline)简洁
List<String> names = markets.stream()
    .map(Market::name)
    .filter(Objects::nonNull)
    .toList();

// ❌ 避免复杂的嵌套流;为了清晰起见,优先使用循环

异常

  • 对于领域错误(Domain errors)使用非检查异常(Unchecked Exceptions);使用上下文包装技术异常
  • 创建领域特定的异常(例如:MarketNotFoundException
  • 避免捕获过于宽泛的 catch (Exception ex)(除非在中心位置重新抛出或记录日志)
java
throw new MarketNotFoundException(slug);

泛型与类型安全

  • 避免使用原始类型(Raw types);声明泛型参数
  • 优先在可重用的工具类中使用受限泛型(Bounded Generics)
java
public <T extends Identifiable> Map<Long, T> indexById(Collection<T> items) { ... }

项目结构 (Maven/Gradle)

src/main/java/com/example/app/
  config/
  controller/
  service/
  repository/
  domain/
  dto/
  util/
src/main/resources/
  application.yml
src/test/java/... (镜像 main 目录)

格式与样式

  • 始终一致地使用 2 或 4 个空格(遵循项目标准)
  • 每个文件仅包含一个 public 顶级类型
  • 保持方法短小且专注;提取助手方法(Helper methods)
  • 成员顺序:常量、字段、构造函数、public 方法、protected、private

应避免的代码异味 (Code Smells)

  • 过长的参数列表 -> 使用 DTO 或建造者模式(Builder)
  • 过深的嵌套 -> 使用早期返回(Early Return)
  • 魔术数字 -> 使用命名常量
  • 静态可变状态 -> 优先使用依赖注入(Dependency Injection)
  • 沉默的 catch 块 -> 记录日志并采取行动,或者重新抛出

日志记录

java
private static final Logger log = LoggerFactory.getLogger(MarketService.class);
log.info("fetch_market slug={}", slug);
log.error("failed_fetch_market slug={}", slug, ex);

Null 处理

  • 仅在万不得已时接受 @Nullable;否则使用 @NonNull
  • 对输入使用 Bean 校验(Bean Validation,如 @NotNull@NotBlank

测试预期

  • JUnit 5 + AssertJ 实现流式断言(Fluent Assertions)
  • 使用 Mockito 进行打桩;尽可能避免使用部分打桩(Partial mocks)
  • 优先选择确定性测试;严禁隐藏的 sleep

记住:保持代码的意图清晰、类型安全且可观测。除非证明确有必要,否则应优先优化可维护性而非微小的性能优化。

Frequently asked questions

What does the Java Coding Standards AI skill do?

适用于 Spring Boot 服务的 Java 编码规范:命名、不可变性、Optional 使用、流(Stream)、异常、泛型及项目布局。

Why use Java Coding Standards on TypingMind?

Because you install it once and use it with any model. Java Coding Standards is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Java Coding Standards in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/xu-xiang/everything-claude-code-zh/tree/main/docs/ja-JP/skills/java-coding-standards. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Java Coding Standards?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Java Coding Standards?

As many as you like. As long as a model supports skills, you can use Java Coding Standards with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Java Coding Standards AI skill free?

Yes. It is published on GitHub by xu-xiang under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇