Java Development Manual (Songshan Edition)
Overview
This manual is based on the Alibaba Java Development Manual (Songshan Edition), and divides the conventions into 7 dimensions. The conventions are divided according to the strength of their binding force into:
| Level |
Meaning |
Description |
| [Mandatory] |
Must be followed |
Violation may cause serious problems |
| [Recommended] |
Recommended to follow |
Improves code quality and maintainability |
| [Reference] |
May be optionally adopted |
Judge according to the actual situation |
Chapter Navigation
Select the detailed conventions of the corresponding chapter according to your needs:
| Chapter |
Applicable Scenarios |
Detailed Documentation |
| Programming Conventions |
Naming, formatting, OOP, concurrency, collection handling |
coding-convention.md |
| Exception Logs |
Error codes, exception handling, logging conventions |
exception-log.md |
| Unit Testing |
Test cases, coverage, Mock |
unit-test.md |
| Security Conventions |
SQL injection, XSS, CSRF, desensitization |
security.md |
| MySQL Database |
Table creation, indexes, SQL, ORM |
mysql.md |
| Engineering Structure |
Layered architecture, dependency management, servers |
project-structure.md |
| Design Conventions |
UML, design patterns, design principles |
design.md |
Quick Reference
Naming Convention Quick Reference
// 类名:UpperCamelCase
public class UserService { }
public class UserDO { } // DO/DTO/VO例外
// 方法名/变量:lowerCamelCase
private String userName;
public void getUserById() { }
// 常量:全大写+下划线
public static final int MAX_RETRY_COUNT = 3;
// 包名:全小写
package com.company.project.service;
Prohibited Items Quick Reference
| Prohibited |
Reason |
| Pinyin naming |
Poor readability |
| Magic values |
Difficult to maintain |
SELECT * |
Performance and maintainability |
| Executors creating thread pools |
Possible OOM |
| String concatenation SQL |
Injection risk |
| return in finally |
Loses try return value |
| remove in foreach |
ConcurrentModificationException |
Must-Do Items Quick Reference
| Must |
Reason |
| Add @Override to overridden methods |
Avoid signature errors |
| Tables must have three fields |
id, create_time, update_time |
| Desensitize sensitive data |
Privacy protection |
| Parameter validation |
Security protection |
| ThreadLocal cleanup |
Avoid memory leaks |
| Use placeholders in logs |
Performance optimization |
Exception Handling Quick Reference
// 正确的异常处理
try {
// 业务逻辑
} catch (SpecificException e) {
logger.error("操作失败, 参数: {}", params, e);
throw new BusinessException("用户友好提示", e);
} finally {
// 资源关闭(JDK7+ try-with-resources)
}
Database Quick Reference
-- 建表必备
CREATE TABLE example (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 索引命名
-- 主键: pk_字段名
-- 唯一: uk_字段名
-- 普通: idx_字段名
Concurrency Handling Quick Reference
// 线程池创建
ThreadPoolExecutor executor = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(queueCapacity),
new ThreadFactory() {
private AtomicInteger counter = new AtomicInteger(1);
public Thread newThread(Runnable r) {
return new Thread(r, "worker-" + counter.getAndIncrement());
}
},
new ThreadPoolExecutor.CallerRunsPolicy()
);
// ThreadLocal使用
try {
threadLocal.set(value);
// 业务逻辑
} finally {
threadLocal.remove(); // 必须回收
}
Usage Guide
Code Review Scenarios
- Naming check → see coding-convention.md the "Naming Style" chapter
- Concurrency issues → see coding-convention.md 's "Concurrent Processing" section
- Exception Handling → View exception-log.md
- Security Issues → View security.md
New Project Setup Scenario
- Architecture Design → View design.md
- Layered Structure → View project-structure.md
- Database Design → View mysql.md
- Unit Testing → View unit-test.md
Troubleshooting Scenario
- NPE Issues → View exception-log.md 's "NPE Protection"
- Performance Issues → View mysql.md 's "Index Guidelines"
- Concurrency Issues → View coding-convention.md 's "Concurrent Processing"