
Understanding Spring Boot Project Structure
This chapter covers, understanding the Spring Boot project structure.
March 22, 2024 · 6 min read
In the previous lesson, we saw different ways to bootstrap a Spring Boot project and we have learnt about different methods available for building and running the project. In this chapter, we will understand the different files and folders present inside the spring boot project.
Spring Boot organizes the code according to some of the best practices in the industry. Since we generated our project with Gradle in the previous chapter, here's what the hello-spring project looks like:
hello-spring
├── build
├── build.gradle
├── settings.gradle
├── gradlew
├── gradlew.bat
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── stacktips
│ │ └── hello_spring
│ │ └── HelloSpringApplication.java
│ └── resources
│ ├── application.properties
│ ├── static
│ └── templates
└── test
└── java
└── com
└── stacktips
└── hello_spring
└── HelloSpringApplicationTests.java
If you generated a Maven project instead in the previous chapter, the layout is the same under src/, but the build tooling differs slightly:
hello-spring
├── target
├── pom.xml
├── mvnw
├── mvnw.cmd
└── .mvn
└── wrapper
└── maven-wrapper.properties
Everything from src/ down works identically regardless of which build tool you picked — only the sections below covering the build file, wrapper, and output folder differ between the two.
build.gradle (or pom.xml)
This is the file we looked at back in Chapter 2 — it outlines the project metadata, dependencies, and build configuration for your project. Gradle projects use build.gradle; Maven projects use pom.xml instead.
settings.gradle
This file declares the root project's name and, in multi-module projects, which sub-modules belong to the build. For a single-module project like ours, it just contains a single line setting the project name (rootProject.name = 'hello-spring'). Maven projects don't have an equivalent file — the same information lives inside pom.xml.
The Gradle/Maven Wrapper
The gradlew and gradlew.bat scripts (or mvnw and mvnw.cmd for Maven) are the wrapper scripts we used in the previous chapter to run the project — ./gradlew bootRun on macOS/Linux, gradlew.bat bootRun on Windows. They let anyone build the project with the exact Gradle (or Maven) version it was built with, without installing Gradle or Maven globally. The supporting jar and version info live in gradle/wrapper/ (or .mvn/wrapper/ for Maven) — you don't need to touch these files directly.
The build (or target) Directory
This directory is generated automatically when you build the project — build/ for Gradle, target/ for Maven. It contains the compiled .class files, the packaged JAR, and other build artifacts. You don't need to manually modify anything in this directory, and it's typically excluded from version control via .gitignore.
The src/main/java Directory
The src/main/java directory contains the Java source code for your project, following the standard Java packaging structure. Recall from Chapter 3 that we set the Spring Initializr Group to com.stacktips and Package name to com.stacktips.hello_spring — that's exactly the folder path you see here: com/stacktips/hello_spring. This is your project's base package, and it's where the rest of your application code will live as the course progresses.
The Main Application Class
The HelloSpringApplication class is the entry point of your Spring Boot application. It contains a main() method, which is responsible for initializing the Spring application context and starting the application.
This class is annotated with @SpringBootApplication, a convenience annotation that combines three other Spring Boot annotations:
@EnableAutoConfigurationenables Spring Boot's auto-configuration mechanism, which automatically configures many common Spring beans based on the dependencies present in the classpath.@Configurationindicates that the class contains one or more bean definitions via@Beanannotations.@ComponentScanscans the classpath for classes annotated with@Component,@Service,@Repository, or@Configuration. Classes registered with any of these four annotations are registered as beans in the Spring application context.
We will learn more about this annotation in Chapter 5, Dependency Injection.
@SpringBootApplication
public class HelloSpringApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringApplication.class, args);
}
}By using @SpringBootApplication, you don't need to declare @Configuration, @EnableAutoConfiguration, and @ComponentScan separately — it bundles all three for you.
The src/main/resources Directory
The resources directory is for non-Java resources your application needs — configuration files, static assets and templates.
application.properties — contains the configuration for your Spring Boot application, as key-value pairs separated by =. We'll cover this in depth in the upcoming Chapter 7, Configuration Management chapter.
resources/static — dedicated to static assets served as-is, such as CSS, JS, and images.
resources/templates — the default location for server-side view templates that render dynamic HTML. Spring Boot natively supports:
- Thymeleaf: the default template engine in Spring Boot, activated through the
spring-boot-starter-thymeleafstarter dependency. - FreeMarker: activated using the
spring-boot-starter-freemarkerstarter dependency. - Mustache: a logic-less templating engine, activated via the
spring-boot-starter-mustachedependency.
All of these template engines automatically resolve template files from resources/templates.
The src/test/java Directory
This directory contains the unit and integration test classes for your project, mirroring the same package structure as src/main/java. Spring Initializr already generated a starter test class, HelloSpringApplicationTests, that simply verifies the application context loads successfully. We'll write our own tests in Chapter 24, Testing Spring Boot REST API using Mockito and MockMvc.
Summary
In this chapter, we walked through every file and folder Spring Initializr generated for us. In the next chapter, we'll look at Dependency Injection in Spring Boot, one of the core concepts that makes Spring Boot applications easy to build and test.
