Standard Project Structure for Spring Boot Projects


In this short 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.
As per Spring boot documentation, the team says 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 Java Packages with Examples.

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.

Project Structure - First approach

The following spring boot project structure shows a typical layout recommended by spring boot team:
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);
    }
}

Static Content

In a spring boot web application, static files like HTML, CSS, JS and IMAGE files can be served directly from any of the following classpath locations out of the box. No configuration required.

Let's look at the resources folder in the above project structure. This directory, as the name suggests, is dedicated to all the static resources, templates and property files.
  • resources/static - contains static resources such as CSS, js, and images.
  • resources/templates - contains server-side templates which are rendered by Spring.
  • resources/application.properties - This file is very important. It contains application-wide properties. Spring reads the properties defined in this file to configure your application. You can define a server’s default port, server’s context path, database URLs etc, in this file.
By default, Spring boot serves static content from one of the following locations in the classpath:
  • /static
  • /public
  • /resources
  • /META-INF/resources
By default, resources are mapped on /**, but you can tune that with the spring.mvc.static-path-pattern property. For instance, relocating all resources to /resources/** can be achieved as follows:

spring.mvc.static-path-pattern=/resources/**

You can also customize the static resource locations by using the spring.resources.static-locations property (replacing the default values with a list of directory locations). The root Servlet context path, "/", is automatically added as a location as well.

Dynamic web content (Templates)

Spring supports the following template engines by default. These templates can be activated using appropriate spring boot starters.
  • FreeMarker - spring-boot-starter-freemarker
  • Groovy - spring-boot-starter-groovy
  • Thymeleaf - spring-boot-starter-thymeleaf
  • Mustache - spring-boot-starter-mustache

All these template engines will resolve their template files from the path src/main/resources/template.
Read more about how to serve static content in spring boot application at Spring Boot Official Doc - Static Content

Project Structure - Second approach

However, above typical layout approach works well but some of the developers prefer to use the following packaging or project structure: 
Separate package for each layer like a model, controller, dao and service etc.

pom.xml

In a spring boot project, most of the modules can be enabled or disabled just by adding a set of starters. All Spring Boot projects typically use spring-boot-starter-parent as the parent in pom.xml.

What is the Spring Boot Starter Parent and How to use it?

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>
spring-boot-starter-parent allows us to manage the following things for multiple child projects and modules:
  • Configuration - Java Version and Other Properties
  • Dependency Management - Version of dependencies
  • Default Plugin Configuration
We should need to specify only the Spring Boot version number on this dependency. If you import additional starters, you can safely omit the version number.
Read more about Spring Boot Starter Parent at Overview of Spring Boot Starter Parent.

Read more about all spring boot starters at Important Spring Boot Starters with Examples.

For a maintainable code:
  • Only add needed starters. This keeps the application lighter. An unwanted starter may lead to extra autowired beans.
  • Most of the starters ship with their own transitive dependencies. So you may never need to specify versions. Most of the IDE’s already highlight this unwanted version tags.
  • Know your starter dependencies. This way, You may never need to write configurations.

Spring boot web applications - using JSP as views

If you are creating spring boot MVC web application using JSP as views then following diagram shows a typical project structure layout. As a best practice, I strongly encourage placing your JSP files in a directory under the 'WEB-INF' directory so there can be no direct access by clients. 

That's all. I think I covered almost all the points regarding spring boot project structure in this article. Let me know if anything needs to be added.
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.
In the next article, you will learn how to develop CRUD RESTFul API using Spring boot 2, Hibernate 5, JPA, Maven, and MySQL database.
Check out all spring boot articles, guides, and tutorials at Top Spring Boot Tutorials

Reference

Comments