Lesson 23 / 25
Project Structure
Package by feature.
Feature packages and thin layers
Organise packages by feature (products, orders, payments), each containing its controller, service, repository and DTOs, rather than global controllers and services packages. Keep controllers thin (HTTP concerns only), put business rules in services, and keep the @SpringBootApplication class at the root and minimal. Spring Modulith can verify module boundaries and document dependencies as the code grows.
Build services that last
Organise code by feature, keep up with Spring Boot releases, and review with a checklist.
A feature-oriented layout
Names are examples.
com.example.shop/
ShopApplication.java minimal main class
products/
ProductController.java HTTP + DTOs
ProductService.java business rules, transactions
ProductRepository.java data access
Product.java entity
orders/ ...
config/ security, properties, infrastructure beansKeep the main class minimal
As the testing pitfall showed, beans on the main class leak into every test slice.
Quick check: What does package-by-feature mean?
- Grouping each feature's controller, service and repository together
- One package per annotation type
- Putting all code in the main class
- A package per developer
Answer
Grouping each feature's controller, service and repository together — Cohesive modules.