Project structure, from layers to modules
Your projects show the full evolution. Package-by-layer, then package-by-feature, then a modular monolith, then Spring Modulith with enforced boundaries.
The concept
The way you organise packages decides which code can depend on which, and so how easily the system can change. Your five projects step through four levels of maturity.
Level 1: package-by-layer
controllers/ services/ repositories/ entities/ dtos/ mappers/
This is easy to start with, but one feature (say “cart”) is spread across six folders, and nothing stops ProductController from reaching straight into UserRepository.
Level 2: package-by-feature
module/booking/{controller,dto,mapper,model,repository,service}
module/room/...
shared/{exception,response,util}
Everything for “booking” lives together. You can understand or delete a feature by looking at one folder.
Level 3: modular monolith (layers inside each module)
module/report/
domain/ <- entities, repositories, domain events
application/ <- services, use cases, schedulers
infrastructure/ <- adapters to the outside world
web/ <- controllers + web/dto
Level 4: enforced modules (Spring Modulith)
ledger/
api/ <- the ONLY public surface (interfaces, events)
application/ <- package-private implementations
domain/ infrastructure/ web/
A test fails the build if another module reaches past api/.
In your projects
controllers/ · services/ · repositories/ · entities/Level 1. Some controllers (ProductController, UserController and AuthController, which injects UserRepository) skip the service layer and call repositories directly. That is a typical layer-based shortcut. The business rules end up in controllers, where nothing else can reuse them.
com.leroy.hostelbackend.module.(feature)Level 2. The modules are analytics, audit, auth, booking, complaint, dashboard, email, hostel, map, notification, preference, review, room, user and waitlist, with cross-cutting code in shared/ and config/. The flow is controller, then service, then repository, and services return DTOs built by MapStruct.
module/(feature)/{domain,application,web,infrastructure}Level 3. About 400 files split into 16 modules (account, report, export, notification, gtec…). shared/ holds annotations, aspects, messaging and security. IR3D follows the same shape at about 820 files and 23 modules.
src/test/.../ModularityTests.javaLevel 4. Modules expose an api/ package (ledger/api/PostingEngine, coa/api/AccountDirectory, company/api/CompanyDirectory), and implementations are package-private (class DefaultPostingEngine implements PostingEngine). The boundaries are verified:
ApplicationModules.of(SperotekAccountingApplication.class).verify();Modules talk to each other through events (JournalPosted, CompanyCreated) rather than direct calls.
Alternatives
| Structure | Good for | Cost |
|---|---|---|
| Package-by-layer | Tutorials, tiny CRUD apps | Features scattered, no boundaries |
| Package-by-feature | Small and medium apps | Boundaries by convention only |
| Modular monolith | Growing products, small teams | Discipline needed |
| Spring Modulith / ArchUnit enforced | Long-lived products | Learning curve, event plumbing |
| Microservices | Many teams, independent scaling and deploys | Network failures, distributed data, ops cost |
| Strict hexagonal / Clean Architecture | Domain-heavy systems | More interfaces and mapping |
Related concepts you must know
- Coupling and cohesion. Aim for high cohesion inside a module and low coupling between modules.
- Hexagonal architecture (ports and adapters). The domain defines ports (interfaces) and the infrastructure implements adapters. IR3D’s
PaymentGateway/ Paystack and SperoTek’sEmailTransport/SmtpEmailTransportare examples (Module 5). - Bounded contexts (DDD). A module is roughly a bounded context with its own language. For example, “Account” means a chart-of-accounts entry in SperoTek but a login elsewhere.
- Dependency direction. The domain should not import web or infrastructure code. Arrows point inward.
- Conway’s Law. System structure mirrors team structure.