Folder Structure Overview
Spring Boot projects follow a layered and modular folder structure that organizes code, resources, and configuration files efficiently. This structure supports the layered architecture and keeps the project maintainable, scalable, and easy to navigate.
A typical Spring Boot project looks like this:
my-spring-app/
│
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/myapp/
│ │ │ ├── MySpringBootApplication.java # Main application class
│ │ │ ├── controller/ # Handles HTTP requests
│ │ │ ├── service/ # Business logic layer
│ │ │ ├── repository/ # Data access layer
│ │ │ ├── model/ # Entities and domain objects
│ │ │ ├── dto/ # Data Transfer Objects
│ │ │ └── config/ # Java-based configuration classes
│ │ │
│ │ └── resources/
│ │ ├── application.properties / application.yml # App configuration
│ │ ├── static/ # CSS, JS, images
│ │ ├── templates/ # View templates (Thymeleaf, JSP)
│ │ └── db/ # SQL scripts or data initialization
│ │
│ └── test/
│ └── java/
│ └── com/example/myapp/ # Unit & integration tests
│
├── pom.xml (Maven) / build.gradle (Gradle) # Build and dependency management
└── README.md # Project description and documentation
Folder Breakdown & Purpose
1. src/main/java
This folder contains all Java source code, organized according to the layered architecture:
- controller/ → Handles HTTP requests and returns responses (REST or MVC endpoints). Uses
@RestController or @Controller.
- service/ → Contains business logic and orchestrates operations between layers. Annotated with
@Service.
- repository/ → Manages database access using Spring Data JPA, JDBC, or other persistence frameworks. Annotated with
@Repository.
- model/ → Entity classes and domain objects representing application data.
- dto/ → Data Transfer Objects for moving data between layers without exposing entities directly.
- config/ → Java-based configuration classes (e.g., Spring Security, custom beans).
- MySpringBootApplication.java → Main entry point annotated with
@SpringBootApplication to bootstrap the application.
Key Points:
- Promotes modularity, maintainability, and testability.
- Keeps business logic separate from controllers and repositories.
- Follows Presentation → Service → Repository → Persistence pattern.
2. src/main/resources
This folder contains all non-Java resources, such as configuration files, templates, static assets, and SQL scripts:
- application.properties / application.yml → Configures database, server port, logging, and other application settings.
- static/ → Stores CSS, JS, images, and other frontend assets automatically served by Spring Boot.
- templates/ → HTML templates for web views (Thymeleaf, JSP, FreeMarker).
- db/ → Optional folder for SQL scripts or database initialization.
- messages.properties → Supports localization and internationalization (i18n).
Key Notes:
- Spring Boot auto-detects resources here at startup.
- Static and template files are served automatically by Spring MVC.
- Configuration files enable auto-configuration for database, logging, and server settings.
3. src/test/java
- Contains unit and integration tests for controllers, services, and repositories.
- Mirrors the main package structure to maintain consistency.
4. Build Files
- pom.xml (Maven) or build.gradle (Gradle) → Manage project dependencies, plugins, and build configurations.
Best Practices
- Follow a layered structure (Controller → Service → Repository → Model/DTO).
- Keep DTOs separate from entities to maintain clean architecture.
- Organize static and template resources logically.
- Use environment-specific properties (
application-dev.properties, application-prod.properties).
- Keep main class and configuration files at the top level for easy access.
Why This Structure Matters
- Modular & Maintainable: Each layer has a clear responsibility.
- Scalable: Add new features or layers without breaking existing code.
- Testable: Supports unit and integration testing efficiently.
- Beginner-Friendly: Standard structure simplifies understanding project flow.
- Production-Ready: Auto-configuration, embedded server, and logging make deployment easy.