Spring Boot Project Structure

A Spring Boot project follows a consistent folder structure. Understanding this layout helps you know exactly where to put every file you create.

The Default Folder Layout

demo/
│
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/estudy247/demo/
│   │   │       └── DemoApplication.java   ← Entry point
│   │   │
│   │   └── resources/
│   │       ├── application.properties     ← Configuration file
│   │       ├── static/                    ← CSS, JS, images
│   │       └── templates/                 ← HTML templates (Thymeleaf)
│   │
│   └── test/
│       └── java/
│           └── com/estudy247/demo/
│               └── DemoApplicationTests.java
│
├── pom.xml                                ← Maven dependency file
└── .mvn/                                  ← Maven wrapper files

Why This Structure Exists

Maven enforces this layout. The convention means every developer on your team knows where to look without reading a manual. src/main/java holds production code. src/test/java holds test code. src/main/resources holds config and static files.

Recommended Package Structure for Real Projects

As your project grows, split code into logical layers inside your base package:

com/estudy247/demo/
│
├── controller/       ← Handles HTTP requests
│   └── UserController.java
│
├── service/          ← Business logic lives here
│   └── UserService.java
│
├── repository/       ← Database operations
│   └── UserRepository.java
│
├── model/            ← Data classes (entities)
│   └── User.java
│
├── dto/              ← Data Transfer Objects (request/response shapes)
│   └── UserDTO.java
│
└── DemoApplication.java

How Data Flows Through the Layers

  Browser / API Client
         │
         ▼
  ┌─────────────┐
  │ Controller  │  ← Receives HTTP request, returns response
  └──────┬──────┘
         │ calls
         ▼
  ┌─────────────┐
  │   Service   │  ← Runs business rules and logic
  └──────┬──────┘
         │ calls
         ▼
  ┌─────────────┐
  │ Repository  │  ← Reads/writes to the database
  └──────┬──────┘
         │
         ▼
      Database

Each layer has one job. The controller does not talk directly to the database. The repository does not contain business rules. This separation makes the code easy to maintain and test.

The application.properties File

This file sits in src/main/resources and controls your app's behavior.

# Change the default port
server.port=9090

# Application name
spring.application.name=demo

# Database connection (example)
spring.datasource.url=jdbc:mysql://localhost:3306/mydb

You can also use application.yml instead of application.properties if you prefer YAML format.

The static and templates Folders

static/

Place files here that browsers download directly — CSS stylesheets, JavaScript files, and images. Spring Boot serves them automatically at the root URL. A file at static/logo.png becomes accessible at http://localhost:8080/logo.png.

templates/

Place Thymeleaf HTML templates here when building server-rendered pages. This topic applies only when you add the Thymeleaf dependency. For pure REST APIs, this folder stays empty.

pom.xml — The Dependency Manifest

Every external library your app uses must appear in pom.xml. Maven reads this file, downloads the required JARs, and adds them to your classpath. You never manage JAR files manually.

Summary

  • Production code lives in src/main/java; test code in src/test/java
  • Configuration and static files live in src/main/resources
  • Split code into controller, service, repository, and model layers
  • Each layer has one responsibility — this keeps code maintainable
  • pom.xml manages all dependencies; Maven downloads them automatically

Leave a Comment

Your email address will not be published. Required fields are marked *