跪拜 Guibai
← All articles
Architecture · Design Patterns · AI Programming

A Spring Boot 4.1 DDD Scaffold Built to Be Read by AI, Not Just Humans

By 刀法如飞 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

AI coding agents produce better output when they have a concrete, well-structured reference to imitate. This scaffold treats its own codebase as the prompt—giving agents a working DDD template with real multi-data source transactions and event-driven wiring, which cuts down on architectural drift in generated code.

Summary

The scaffold enforces a clean separation across interfaces, application, domain, and infrastructure layers, with MySQL and PostgreSQL running under independent transaction managers. Domain models are kept free of framework annotations, repository interfaces live in the domain layer, and implementations sit in infrastructure—a structure meant to give AI agents unambiguous boundaries to follow.

It ships with JdbcClient for data access, MyBatis Plus for complex queries, and RocketMQ 5.3 for domain event publishing with graceful degradation. A full Agent CLI guide is included, showing how to point Claude Code or Cursor Agent at the scaffold as a reference project and generate new aggregates, repositories, and events that conform to the same conventions.

The pitch is that architecture setup drops from one to two weeks down to about ten minutes, and that the codebase doubles as a specification an AI can read to stay on track. The project lives inside a larger design-patterns repository and targets teams adopting AI-assisted development who want generated code to land inside a disciplined DDD structure.

Takeaways
Uses Spring Boot 4.1 and JDK 25 with a strict four-layer DDD structure: interfaces, application, domain, infrastructure.
Domain models contain no Spring annotations; repository interfaces are defined in the domain layer and implemented in infrastructure.
MySQL and PostgreSQL run as independent data sources with separate HikariCP pools and transaction managers.
JdbcClient is the primary data-access API, with MyBatis Plus available for complex queries.
RocketMQ 5.3 publishes domain events with topic isolation and graceful degradation when the broker is unavailable.
An Agent CLI guide shows how to reference the scaffold in tools like Claude Code to generate new aggregates, repositories, and events that follow the same conventions.
Architecture setup is claimed to drop from 1–2 weeks to roughly 10 minutes by cloning and configuring the scaffold.
Conclusions

Treating a codebase as a machine-readable specification for AI agents is a pragmatic shift: the architecture itself becomes the prompt, reducing the need to describe conventions in natural language.

Keeping domain models completely free of framework annotations is a hard rule that many real-world DDD projects relax; enforcing it here gives AI agents a clearer signal about where business logic ends and infrastructure begins.

The scaffold's value is less about the individual technology choices and more about the consistent structure it imposes—AI tools are sensitive to pattern regularity, and a disciplined codebase produces more predictable output than a loose set of guidelines.

Concepts & terms
DDD Four-Layer Architecture
A Domain-Driven Design pattern that separates code into Interface (controllers, DTOs), Application (service orchestration, commands), Domain (aggregate roots, domain services, repository interfaces), and Infrastructure (repository implementations, messaging, persistence) layers.
JdbcClient
A fluent, type-safe JDBC API introduced in Spring Framework 6.1 that simplifies parameter binding and result mapping compared to the older JdbcTemplate.
Aggregate Root
In DDD, the single entry point to a cluster of domain objects that must be treated as a unit for data changes. The scaffold's User and Order classes are aggregate roots that enforce invariants and record domain events.
Graceful Degradation (RocketMQ)
The scaffold configures RocketMQ to allow the application to start normally even when the message broker is unreachable, logging a warning instead of failing.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗