Spring Boot Project, Package and Code Structure - RameshMF/spring-boot-developers-guide GitHub Wiki
In this article, we will discuss how to create a standard spring boot project structure or packaging structure. I will show you recommended ways to create spring boot project structure so this will help beginners to maintain standard coding structure.
Notice that Spring Boot does not require any specific code layout to work. However, there are some best practices that help.
Don't use the “default” Package
When a class does not include a package declaration, it is considered to be in the “default package”. The use of the “default package” is generally discouraged and should be avoided. It can cause particular problems for Spring Boot applications that use the @ComponentScan, @EntityScan, or @SpringBootApplication annotations since every class from every jar is read.
I recommend that you follow Java’s recommended package naming conventions and use a reversed domain name (for example, com.javaguides.projectname).
To know standard package naming conventions, check out - about http://www.javaguides.net/2018/09/packages-in-java-with-examples.html
Typical Layout
Spring boot team generally recommend that you locate your main application class in a root package above other classes. The @SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @SpringBootApplication annotated class is used to search for @Entity items. Using a root package also allows the component scan to apply only on your project.
Tip
If you don’t want to use @SpringBootApplication, the @EnableAutoConfiguration and @ComponentScan annotations that it imports defines that behaviour so you can also use that instead.
The following spring boot project structure shows a typical layout recommended by spring boot team:
diagram here
The Application.java file would declare the main method, along with the basic @SpringBootApplication, as follows:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
However, above typical layout works well but some of the developers prefer use following packaging or project structure:
diagram here
I suggest either of these structuring your project code works well.
If you have any suggestion about the above approaches then leave your comments in the comment section.