Recommended Package Structure of a Spring Boot Project

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:

@SpringBootApplication(scanBasePackages = "example.maixuanviet.com")
@EnableJpaRepositories("example.maixuanviet.com")
@EntityScan("example.maixuanviet.com")

Also, be aware that extra configuration might be needed.

4. Design

The design of the package structure is independent of Spring Boot. Therefore, it should be imposed by the requirements of our project.

One popular strategy is package-by-feature, which enhances modularity and enables package-private visibility inside sub-packages.

Let’s take, for example, the PetClinic project. This project was built by Spring developers to illustrate their view on how a common Spring Boot project should be structured.

It’s organized in a package-by-feature manner. Hence, we have the main package, org.springframework.samples.petclinic, and 5 sub-packages:

  • org.springframework.samples.petclinic.model
  • org.springframework.samples.petclinic.owner
  • org.springframework.samples.petclinic.system
  • org.springframework.samples.petclinic.vet
  • org.springframework.samples.petclinic.visit

Each of them represents a domain or a feature of the application, grouping highly-coupled classes inside and enabling high cohesion.

5. Conclusion

In this small article, we had a look at some recommendations we need to keep in mind when building a Spring Boot project – and learned about how we can design the package structure.

Related posts:

Java Program to Implement the String Search Algorithm for Short Text Sizes
Giới thiệu Google Guice – Binding
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java – Convert File to InputStream
Java String to InputStream
Guide to Java OutputStream
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Tips for dealing with HTTP-related problems
Constructor Injection in Spring with Lombok
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Partition a List in Java
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java – Write to File
A Guide to WatchService in Java NIO2
Java Program to Check for balanced parenthesis by using Stacks
Exception Handling in Java
HttpAsyncClient Tutorial
Java Program to Implement Knapsack Algorithm
Request Method Not Supported (405) in Spring
ETags for REST with Spring
Hướng dẫn Java Design Pattern – Visitor
Java Program to Find Strongly Connected Components in Graphs
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Một số nguyên tắc, định luật trong lập trình
A Comparison Between Spring and Spring Boot
Một số từ khóa trong Java
Spring’s RequestBody and ResponseBody Annotations
Java – InputStream to Reader
Creating Docker Images with Spring Boot
Java Program to Implement a Binary Search Tree using Linked Lists
Netflix Archaius with Various Database Configurations