StackTips

Understanding Spring Boot Project Structure

nilan avtar

Written by:

Nilanchala,  12 min read,  updated on March 22, 2024

Spring Boot is an opinionated framework and it comes with a default project structure that organizes your code based on some of the industry's best practices.

In the previous chapter, we created a Spring boot project using the Spring CLI and the Spring initializer. In both cases, we have used the same configuration for generating our project.

Now let us unzip the project, and open it in the IntelliJ IDE. After unzipping the project, you’ll see a project structure similar to what is shown in the following picture;

movies-api
├── HELP.md
├── build
│   └── libs
│       ├── movies-api-0.0.1-SNAPSHOT-plain.jar
│       └── movies-api-0.0.1-SNAPSHOT.jar
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── stacktips
    │   │           └── app
    │   │               └── MoviesApiApplication.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── com
                └── stacktips
                    └── app
                        └── MoviesApiApplicationTests.java

A typical Spring Boot project includes the following folders and files.

pom.xml (Maven) or build.gradle (Gradle)

You will either have a pom.xml file or build.gradle file based on the build tool used for your project. This file defines the project metadata, and dependencies for your project and contains the project build configurations.

While generating this project we have selected Gradle as the build tool, hence we see build.gradle file in the root directory.

The src/main/java Directory

The src/main/java directory contains the Java source code files for your project. It follows the standard Java packaging structure, where the root package is called as the base package.

The src/test/java Directory

This directory contains the unit and integration test classes for your project.

The build Directory

This directory is automatically generated by Maven (or Gradle) and contains the compiled Java classes, JAR files, and other build artifacts. We don't need to manually modify anything in this directory.

If your source code is tracked in git, this directory is generally not tracked and added to the .gitignore file.

The Main Application Class

The MoviesAPIApplication class is the entry point of your Spring Boot application and contains a main() method. It is responsible for initializing the Spring application context and starting the Spring application.

This class is annotated with @SpringBootApplication annotation.

The @SpringBootApplication annotation is a convenience annotation that combines 3 other Spring Boot annotations.

  • The @EnableAutoConfiguration enables Spring Boot's auto-configuration mechanism, which automatically configures many common Spring beans based on the dependencies that are present in the classpath.
  • The @Configuration annotation indicates that the class contains one or more bean definitions containing @Bean annotations.
  • The @ComponentScan annotation scans the classpath for classes annotated with @Component, @Service, @Repository, or @Configuration annotations. Classes that are registered with any of the 4 annotations are registered as beans in the Spring application context.

The inner workings of Dependency Injection in Spring Boot are discussed at length in Chapter 4.

By using the @SpringBootApplication annotation, you can simplify the configuration of your Spring Boot application as you don't need to declare the @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations separately.

@SpringBootApplication
public class HelloSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloSpringBootApplication.class, args);
    }
}

The src/main/resources directory

The resources directory is for non-Java resources that your application needs, such as configuration files, static assets (e.g., HTML, CSS, JavaScript), templates, and other resources.

The Application Properties Class

The application.properties file contains the configurations for your Spring Boot applications. It is located in the src/main/resources directory. The properties file contains key-value pairs separated by equals =, where the key is the name of the property and the value is the value of the property.

The application.properties file contains all the configurations such as:

  • Configuring the server port that the application should run
  • The database connection-related configuration such as database URL, username, and password
  • SMTP email server settings
  • To set the log levels
server.port=8080
server.contextPath=/

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.password=root
spring.datasource.username=admin
spring.datasource.type=mysql

During the Spring boot context initialization, the properties defined in the application.properties file are used to conditionally create or configure Spring beans.

Static Content

The resources/static folder is dedicated to all the static resources, templates, and property files. This folder contains static resources such as CSS, JS, and images.

resources/templates

The resources/templates directory serves as the default location for storing the server-side view templates that are used for rendering dynamic content on web pages. These templates are processed by a templating engine to generate the final HTML output before sending a response to the browser.

Spring Boot supports the following template engines by default.

  • Thymeleaf: This is the default template engine used in Spring Boot and activated using spring-boot-starter-thymeleaf starter dependency.
  • FreeMarker: FreeMarker is another popular option and can be activated using spring-boot-starter-freemarker starter dependency.
  • Mustache: A logic-less templating engine focusing on simplicity and separation of concerns. activated using spring-boot-starter-mustache starter dependency.

All these template engines automatically resolve the template files from the /resources/template directory automatically.

Getting Started with Python - A Beginners Guide

Python is a language with a simple syntax and a powerful set of libraries. It is an interpreted language, with a rich programming environment, including a robust debugger and profiler.

>> CHECK OUT THE COURSE

Continue reading..