java-conventions
Java coding conventions for enterprise applications. Covers package naming, class naming suffixes, method ordering, null handling, and immutability patterns.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Java Conventions
Package Naming
com.{org}.{domain}.{layer}
| Layer | Package | Example |
|---|---|---|
| API/Controller | .controller |
com.forge.order.controller |
| Service | .service |
com.forge.order.service |
| Repository | .repository |
com.forge.order.repository |
| Domain/Model | .model |
com.forge.order.model |
| DTO | .dto |
com.forge.order.dto |
| Configuration | .config |
com.forge.order.config |
| Exception | .exception |
com.forge.order.exception |
Class Naming Suffixes
| Type | Suffix | Example |
|---|---|---|
| REST Controller | Controller |
OrderController |
| Service | Service |
OrderService |
| Repository | Repository |
OrderRepository |
| DTO (request) | Request |
CreateOrderRequest |
| DTO (response) | Response |
OrderResponse |
| Configuration | Config |
SecurityConfig |
| Exception | Exception |
OrderNotFoundException |
| Mapper | Mapper |
OrderMapper |
| Event | Event |
OrderCreatedEvent |
Method Ordering in Classes
- Static fields (logger, constants)
- Instance fields (final dependencies)
- Constructor
- Public API methods (business operations)
- Package-private / protected methods
- Private helper methods
- Static factory/utility methods
Null Handling
- Return
Optional<T>for methods that might not return a value — NEVER returnnullfrom public methods - Use
@NonNull/@Nullableannotations at entry points - NEVER return
nullfor collections — return empty collections
// GOOD
public Optional<Order> findById(UUID id) {
return orderRepository.findById(id);
}
Immutability
- Prefer
finalfields wherever possible - Use Java Records for DTOs (Java 16+)
- Use
Collections.unmodifiableList()for collection fields
public record CreateOrderRequest(
@NotNull String customerId,
@NotEmpty List<OrderItemRequest> items
) {}
Import Ordering
-
java.* -
javax.* -
org.* -
com.* - Static imports last
No wildcard imports — always import specific classes.
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-03-11
- 索引更新时间
- 2026-03-11
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!