<< All versions
Skill v1.0.1
currentAutomated scan100/100majiayu000/claude-skill-registry-data/java-dev-majiayu000-claude-skill-regist
3 files
──Details
PublishedMay 26, 2026 at 01:20 PM
Content Hashsha256:3da1171a1c20cbb2...
Git SHAcb443017503a
Bump Typepatch
──Files
Files (1 file, 4.8 KB)
SKILL.md4.8 KBactive
SKILL.md · 219 lines · 4.8 KB
name: java-dev description: Java 开发规范,包含命名约定、异常处理、Spring Boot 最佳实践等 version: v3.0 paths:
- "**/*.java"
- "**/pom.xml"
- "**/build.gradle"
- "**/build.gradle.kts"
Java 开发规范
参考来源: Google Java Style Guide、阿里巴巴 Java 开发手册
工具链
bash
# Mavenmvn clean compile # 编译mvn test # 运行测试mvn verify # 运行所有检查# Gradle./gradlew build # 构建./gradlew test # 运行测试
命名约定
| 类型 | 规则 | 示例 | |
|---|---|---|---|
| 包名 | 全小写,域名反转 | com.example.project | |
| 类名 | 大驼峰,名词/名词短语 | UserService, HttpClient | |
| 方法名 | 小驼峰,动词开头 | findById, isValid | |
| 常量 | 全大写下划线分隔 | MAX_RETRY_COUNT | |
| 布尔返回值 | is/has/can 前缀 | isActive(), hasPermission() |
类成员顺序
java
public class Example {// 1. 静态常量public static final String CONSTANT = "value";// 2. 静态变量private static Logger logger = LoggerFactory.getLogger(Example.class);// 3. 实例变量private Long id;// 4. 构造函数public Example() { }// 5. 静态方法public static Example create() { return new Example(); }// 6. 实例方法(公共 → 私有)public void doSomething() { }private void helperMethod() { }// 7. getter/setter(或使用 Lombok)}
异常处理
java
// ✅ 好:捕获具体异常,添加上下文try {user = userRepository.findById(id);} catch (DataAccessException e) {throw new ServiceException("Failed to find user: " + id, e);}// ✅ 好:资源自动关闭try (InputStream is = new FileInputStream(file)) {// 使用资源}// ❌ 差:捕获过宽catch (Exception e) { e.printStackTrace(); }
空值处理
java
// ✅ 使用 Optionalpublic Optional<User> findById(Long id) {return userRepository.findById(id);}// ✅ 参数校验public void updateUser(User user) {Objects.requireNonNull(user, "user must not be null");}// ✅ 安全的空值处理String name = Optional.ofNullable(user).map(User::getName).orElse("Unknown");
并发编程
java
// ✅ 使用 ExecutorServiceExecutorService executor = Executors.newFixedThreadPool(10);Future<Result> future = executor.submit(() -> doWork());// ✅ 使用 CompletableFutureCompletableFuture<User> future = CompletableFuture.supplyAsync(() -> findUser(id)).thenApply(user -> enrichUser(user));// ❌ 差:直接创建线程new Thread(() -> doWork()).start();
测试规范 (JUnit 5)
java
class UserServiceTest {@Test@DisplayName("根据 ID 查找用户 - 用户存在时返回用户")void findById_whenUserExists_returnsUser() {// givenwhen(userRepository.findById(1L)).thenReturn(Optional.of(expected));// whenOptional<User> result = userService.findById(1L);// thenassertThat(result).isPresent();assertThat(result.get().getName()).isEqualTo("test");}}
Spring Boot 规范
java
// ✅ 构造函数注入@Service@RequiredArgsConstructorpublic class UserService {private final UserRepository userRepository;private final EmailService emailService;}// ✅ REST Controller@RestController@RequestMapping("/api/users")public class UserController {@GetMapping("/{id}")public ResponseEntity<UserDto> findById(@PathVariable Long id) {return userService.findById(id).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());}}
性能优化
| 陷阱 | 解决方案 | |
|---|---|---|
| N+1 查询 | 使用 JOIN FETCH 或批量查询 | |
| 循环拼接字符串 | 使用 StringBuilder | |
| 频繁装箱拆箱 | 使用原始类型流 | |
| 未指定集合初始容量 | new ArrayList<>(size) |
日志规范
java
// ✅ 参数化日志log.debug("Finding user by id: {}", userId);log.info("User {} logged in successfully", username);log.error("Failed to process order {}", orderId, exception);// ❌ 差:字符串拼接log.debug("Finding user by id: " + userId);
详细参考
| 文件 | 内容 | |
|---|---|---|
references/java-style.md | 命名约定、异常处理、Spring Boot、测试规范 | |
references/collections.md | 不可变集合(Guava)、字符串分割 | |
references/concurrency.md | 线程池配置、CompletableFuture 超时 | |
references/code-patterns.md | 卫语句、枚举优化、策略工厂模式 |
📋 本回复遵循:java-dev- [具体章节]