Chapter 2 · Practice 2 of 4
Package by feature, with the application class at the root
Why it matters
@SpringBootApplication scans its own package and every package below it. Put the main class in the root package (com.example.shop) so components are found without extra @ComponentScan configuration.
Below the root, group code by feature rather than by layer:
| By layer (harder to change) | By feature (easier to change) |
|---|---|
controller/, service/, repository/ | orders/, payments/, catalog/ |
| One change touches three folders | One change stays in one folder |
Everything must be public | Internals can be package-private |
Package-private classes are a cheap way to stop other features from reaching into internals. Tools such as Spring Modulith can verify those boundaries in a test.