Recommended Package Structure of a Spring Boot Project

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

1. Overview

When building a new Spring Boot project, there's a high degree of flexibility on how we can organize our classes.

Still, there are some recommendations that we need to keep in mind.

2. No Default Package

Given the fact that Spring Boot annotations like @ComponentScan, @EntityScan, @ConfigurationPropertiesScan and @SpringBootApplication use packages to define scanning locations, it's recommended that we avoid using the default package — that is, we should always declare the package in our classes.

3. Main Class

The @SpringBootApplication annotation triggers component scanning for the current package and its sub-packages. Therefore, a solid way to go is to have the main class of the project reside in the base package.

This is configurable, and we can still locate it elsewhere by specifying the base package manually. However, in most cases, this option is certainly simpler.

Even more, a JPA-based project would need to have a few additional annotations on the main class: