Spring Boot Best Practices

Chapter 2 · Practice 2 of 4

Package by feature, with the application class at the root

From Structure and dependency injection

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 foldersOne change stays in one folder
Everything must be publicInternals 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.