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
@RestControlleror@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
@SpringBootApplicationto 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.
