Structure of Spring Boot Application | What's Inside Your Spring Boot App?
This comprehensive guide breaks down the directory layout, configuration files, build mechanics, and packaging internals of a Spring Boot application generated via Spring Initializr. Understanding this internal structure is essential before writing production-grade business logic.
1. Why Do We Need to Understand Project Structure?
When you create a Spring Boot project using Spring Initializr, a complete directory tree containing code folders, configuration templates, build descriptors, and hidden metadata is automatically generated.
- The Problem: Beginners often look at a newly generated Spring Boot project and feel overwhelmed by dozens of mysterious files and folders (
.idea,.mvn,mvnw,pom.xml,src/main, etc.). - The Solution: Knowing the exact role of every file prevents configuration errors, ensures proper dependency management, and clarifies how raw Java source code transforms into a self-contained, runnable executable artifact.
2. Overview of the Generated Directory Tree
Let's examine the top-level files and folders created inside a standard Spring Boot project workspace:
3. Non-Code & IDE Metadata Files
Before exploring the core source code, several helper files manage the developer environment and version control:
.idea/: Internal configuration directory generated by IntelliJ IDEA. It stores workspace settings, compiler options, and module paths. It is strictly local to the developer's machine and is never deployed..mvn/: Contains Maven Wrapper configuration (maven-wrapper.properties), ensuring that anyone building the project uses the exact specified Maven version without requiring a global installation on their local machine.mvnw/mvnw.cmd: The Maven Wrapper executable scripts. If Maven is not installed on your operating system, running./mvnw clean packageautomatically downloads and uses the correct Maven engine..gitignore: Tells Git which files and directories to ignore during version control commits. Crucially, it excludes.idea/, local IDE settings, and thetarget/build folder so that compiled binaries are never pushed to remote repositories.HELP.md: A boilerplate Markdown document containing links to official Spring documentation, reference guides, and starter starter kits.
4. Inside the src/ Directory: Main vs. Test
The heart of any Spring Boot project resides inside the src/ (Source) folder, which is strictly partitioned into production code and test suites:
A. src/main/java/
This is where all your domain models, controllers, services, repositories, and the main Spring Boot application starter class live.
- Example entry point:
com.example.myfirstproject.MyFirstProjectApplication.java. - Contains the
public static void main(String[] args)method which bootstraps the embedded web server and initializes the Spring IoC container.
B. src/main/resources/
This folder holds non-code assets and configuration files required at runtime:
static/: Contains static web assets such as images, stylesheets (.css), client-side JavaScript files, and icons.templates/: Historically used for server-side view rendering engines (like Thymeleaf or JSP) to generate dynamic HTML pages. (In modern REST API architectures, this folder is often left empty).application.properties(orapplication.yml): The central configuration hub of your Spring Boot application. Here you define database connection strings, server port configurations, logging levels, and custom key-value pairs.PROPERTIES# Example configuration in application.properties server.port=8080 spring.data.mongodb.uri=mongodb://localhost:27017/mydb
C. src/test/
Mirrors the structure of src/main, but is dedicated entirely to testing. Here you write JUnit tests, Spring Boot slice tests (@WebMvcTest), and integration tests to verify your business logic.
5. The pom.xml File (Project Object Model)
The pom.xml file is the XML manifesto of Maven projects. It dictates how the project is built, which external libraries (dependencies) are pulled in, and how the compiler behaves.
Core Structure of pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 1. Parent Starter Configuration -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 2. Project Metadata -->
<groupId>com.example</groupId>
<artifactId>my-first-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-first-project</name>
<description>Demo project for Spring Boot</description>
<!-- 3. Global Properties -->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 4. External Dependencies -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 5. Build Plugins -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6. Deep Dive into pom.xml Components
1. The Parent Starter (<parent>)
By inheriting from spring-boot-starter-parent (e.g., version 2.7.15), your project automatically inherits sensible default configurations, dependency management versions (so you don't need to specify version numbers for standard libraries), and plugin management rules.
2. Properties (<properties>)
Allows you to define variables across your build configuration. For instance, setting <java.version>1.8</java.version> instructs the Maven Compiler Plugin to compile source code and target bytecode compatible with Java 1.8.
3. Dependencies (<dependencies>)
External libraries required by your application. Starters like spring-boot-starter-web bundle dozens of transitive dependencies (such as Spring MVC, Jackson for JSON parsing, and the embedded Tomcat server) out of the box.
4. Build Plugins & Re-Packaging (<build>)
The <plugins> section houses build tools. The most important plugin in a Spring Boot application is spring-boot-maven-plugin.
7. Understanding Build Output: target/ and "Fat JARs"
When you execute the Maven build command:
mvn clean package
Maven compiles your code, runs tests, and creates a target/ directory containing two JAR files. This process involves a specialized mechanism known as Re-Packaging.
Real-Life Analogy: Standard JAR vs. Spring Boot "Fat" JAR
Imagine packing for a trip:
- Original JAR: Like carrying a set of clothing patterns and sewing instructions, but leaving all the actual fabric and buttons behind. It contains only your compiled
.classfiles. If you try to run it on a bare machine, it fails because external libraries (like Tomcat or Spring framework classes) are missing. - Spring Boot Fat JAR: Like a fully equipped RV containing beds, a kitchen, fuel, and supplies. It is self-contained. It includes your compiled code plus all embedded dependencies and an embedded web server.
How Re-Packaging Works Under the Hood
- Initial Compilation: The Maven compiler compiles your Java source files into standard thin JAR format inside the
target/folder (saving the original thin artifact). - Re-Packaging Phase: The
spring-boot-maven-pluginintercepts the build lifecycle, gathers all required third-party dependency JARs from your project, injects a specialized Spring Boot class loader, and wraps everything into a single Fat JAR. - Execution: You can take this single Fat JAR to any server with a Java Runtime Environment (JRE) installed and launch it immediately without installing an external application server like Apache Tomcat or WildFly:
BASH
java -jar target/my-first-project-0.0.1-SNAPSHOT.jar
8. Summary and Key Takeaways
- Spring Initializr Layout: Generates a standardized directory structure separating IDE configurations (
.idea), build tools (.mvn,mvnw,pom.xml), and source code (src/main). - Production vs. Test Separation:
src/mainhouses business logic and resource files (application.properties, static assets), whilesrc/testholds test suites. pom.xmlas Manifesto: Acts as the project control center, defining project metadata, parent inheritance (spring-boot-starter-parent), Java compiler versions, and external dependencies.- The Role of Parent Starter: Eliminates version boilerplate by providing curated, tested dependency versions for the entire Spring ecosystem.
- Fat JAR Architecture: The
spring-boot-maven-pluginperforms re-packaging, bundling compiled bytecode and all necessary libraries into a single self-contained executable JAR. - Zero External Server Dependency: Because Spring Boot packages an embedded servlet container (like Tomcat) inside the Fat JAR, applications run standalone via simple command-line execution (
java -jar).