Java Spring Development Best Practices: 14 Tips for Clean and Efficient Code
This article presents fourteen practical Java Spring development best‑practice tips—including using @ConfigurationProperties, Lombok's @RequiredArgsConstructor, modularizing code, throwing exceptions instead of returning error codes, minimizing unnecessary database calls, avoiding null returns, reducing if‑else chains, and leveraging IDE features—to help developers write cleaner, more maintainable backend code.
1. Define Configuration File Information
Store common variables in a YAML configuration file and bind them to a POJO using @ConfigurationProperties instead of @Value .
@Data
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
private String name;
private String website;
private String qq;
private String phoneNumber;
}Inject the bean where needed.
@RestController
@RequiredArgsConstructor
public class PropertyController {
private final DeveloperProperty developerProperty;
@GetMapping("/property")
public Object index() {
return developerProperty.getName();
}
}2. Use @RequiredArgsConstructor Instead of @Autowired
Prefer constructor injection for beans; Lombok's @RequiredArgsConstructor automatically generates the required constructor, eliminating the need for @Autowired .
3. Code Modularization
Keep individual methods under 50 lines (as suggested by Alibaba's Java development handbook) and split responsibilities so each method handles a single piece of logic, making code reusable and easier to maintain.
4. Throw Exceptions Instead of Returning Error Objects
Replace scattered error‑return patterns with meaningful exceptions to keep business logic clean.
5. Reduce Unnecessary Database Queries
Avoid extra DB calls; for example, when deleting a service, first check the status without an additional query if possible.
6. Do Not Return Null
Return empty collections or Optional objects instead of null to prevent NullPointerExceptions.
7. Minimize if‑else Chains
Replace long if‑else if ladders with the Strategy pattern or other polymorphic solutions.
8. Reduce Business Logic in Controllers
Move core logic to the service layer, keeping controllers thin and focused on request handling.
9. Make Full Use of IntelliJ IDEA
Leverage IDEA's code inspections and quick‑fix suggestions, such as converting anonymous classes to lambda expressions.
10. Read Source Code
Regularly study high‑quality open‑source projects (e.g., GitHub repos with >1000 stars) to learn design ideas and advanced APIs, which also helps in interviews.
11. Apply Design Patterns
Familiarize yourself with the 23 classic design patterns and apply them where appropriate to produce clean, extensible code.
12. Embrace New Knowledge
Continuously explore technologies beyond daily CRUD tasks; build side demos to practice unfamiliar concepts.
13. Basic Issues
Examples include efficient map traversal and using Optional for null‑checking.
HashMap
map = new HashMap<>();
map.put("name", "du");
for (Map.Entry
entry : map.entrySet()) {
// process entry
} public List
getChild(String pid) {
if (V.isEmpty(pid)) {
pid = BasicDic.TEMPORARY_DIRECTORY_ROOT;
}
CatalogueTreeNode node = treeNodeMap.get(pid);
return Optional.ofNullable(node)
.map(CatalogueTreeNode::getChild)
.orElse(Collections.emptyList());
}14. Check Element Existence Efficiently
Prefer HashSet over ArrayList for existence checks to achieve O(1) lookup.
HashSet
set = new HashSet<>();
// add elements
boolean exists = set.contains("a");These tips collectively aim to improve code readability, maintainability, and performance in Java backend projects.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.