TExcelTAD
Home › Spring Boot › Project Structure

Project Structure

A generated Spring Boot project follows the standard Maven/Gradle layout. Knowing what lives where — and why the main class sits in the root package — saves a lot of confusion later.

The layout

text
tasks/
├── mvnw, mvnw.cmd              Maven wrapper scripts
├── pom.xml                     build file: dependencies and plugins
└── src/
    ├── main/
    │   ├── java/com/example/tasks/
    │   │   ├── TasksApplication.java     entry point (@SpringBootApplication)
    │   │   ├── task/                     feature package
    │   │   │   ├── TaskController.java
    │   │   │   ├── TaskService.java
    │   │   │   ├── TaskRepository.java
    │   │   │   └── Task.java
    │   │   └── config/
    │   │       └── WebConfig.java
    │   └── resources/
    │       ├── application.properties    configuration
    │       ├── static/                   files served as-is (index.html, css, js)
    │       └── templates/                server-side templates (Thymeleaf)
    └── test/java/com/example/tasks/
        └── TasksApplicationTests.java

After a build, compiled classes and the runnable JAR appear in target/ (Maven) or build/ (Gradle). Never commit those folders.

The main class

java
package com.example.tasks;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TasksApplication {
    public static void main(String[] args) {
        SpringApplication.run(TasksApplication.class, args);
    }
}

@SpringBootApplication is a shortcut for three annotations:

Annotation Effect
@SpringBootConfiguration marks the class as a source of bean definitions
@EnableAutoConfiguration lets Boot configure beans based on the classpath
@ComponentScan scans this package and all sub-packages for components

The last row is the important one: component scanning starts at the package of the main class. A controller in com.example.tasks.task is found; a controller in com.example.other is silently ignored. Keep the main class in the root package and everything else below it.

The scanning rule is simple prefix matching on package names, which this plain-Java model shows:

java · try it yourself
import java.util.List;

class Main {
    static boolean scanned(String basePackage, String className) {
        String pkg = className.substring(0, className.lastIndexOf('.'));
        return pkg.equals(basePackage) || pkg.startsWith(basePackage + ".");
    }

    public static void main(String[] args) {
        String base = "com.example.tasks";
        List<String> classes = List.of(
            "com.example.tasks.TasksApplication",
            "com.example.tasks.task.TaskController",
            "com.example.tasks.config.WebConfig",
            "com.example.tasksextra.Helper",
            "com.example.other.ReportController");
        for (String c : classes) {
            System.out.printf("%-40s %s%n", c, scanned(base, c) ? "found" : "IGNORED");
        }
    }
}
com.example.tasks.TasksApplication       found
com.example.tasks.task.TaskController    found
com.example.tasks.config.WebConfig       found
com.example.tasksextra.Helper            IGNORED
com.example.other.ReportController       IGNORED

Package by feature, not by layer

Two common ways to organise code:

By layer By feature (recommended)
controller/TaskController task/TaskController
controller/UserController task/TaskService
service/TaskService task/TaskRepository
service/UserService user/UserController
repository/... user/UserService

Packaging by feature keeps everything about one concept together, lets you use package-private visibility to hide internals, and makes a feature easy to move or delete.

A typical request flow

Most REST applications use three layers:

text
HTTP request
   -> @RestController   (web: parse input, return JSON)
   -> @Service          (business rules, transactions)
   -> Repository        (database access)

Here is the same flow in plain Java — each layer only talks to the one below it:

java · try it yourself
import java.util.ArrayList;
import java.util.List;

class TaskRepository {
    private final List<String> rows = new ArrayList<>();
    void save(String title) { rows.add(title); }
    List<String> findAll() { return List.copyOf(rows); }
}

class TaskService {
    private final TaskRepository repo;
    TaskService(TaskRepository repo) { this.repo = repo; }
    void create(String title) {
        if (title.isBlank()) throw new IllegalArgumentException("title must not be blank");
        repo.save(title.strip());
    }
    List<String> all() { return repo.findAll(); }
}

class TaskController {
    private final TaskService service;
    TaskController(TaskService service) { this.service = service; }
    String post(String body) {
        try { service.create(body); return "201 Created"; }
        catch (IllegalArgumentException e) { return "400 Bad Request: " + e.getMessage(); }
    }
    String get() { return "200 OK " + service.all(); }
}

class Main {
    public static void main(String[] args) {
        TaskController web = new TaskController(new TaskService(new TaskRepository()));
        System.out.println(web.post("  Write docs "));
        System.out.println(web.post("   "));
        System.out.println(web.get());
    }
}
201 Created
400 Bad Request: title must not be blank
200 OK [Write docs]

Resources and tests

  • src/main/resources is on the classpath. application.properties (or application.yml) is loaded automatically.
  • Files in static/ are served at the root URL: static/index.html becomes http://localhost:8080/.
  • src/test/java mirrors the main package. The generated TasksApplicationTests contains a contextLoads() test that fails if the application context cannot start — a cheap but valuable smoke test.
bash · try it yourself
./mvnw test | grep -E "Tests run|BUILD"
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.315 s -- in com.example.tasks.TasksApplicationTests
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

Tip: If an endpoint returns 404 although the controller looks correct, the first thing to check is the controller's package: it must be inside the main class's package.

Test yourself

Your main class is in com.shop.app. Which controller will component scanning find?

  1. com.shop.web.OrderController
  2. com.shop.app.order.OrderController
  3. com.shopapp.OrderController
  4. org.shop.app.OrderController

Which three annotations does @SpringBootApplication combine?

  1. @Controller, @Service, @Repository
  2. @SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan
  3. @Configuration, @Bean, @Value
  4. @EnableWebMvc, @EnableJpa, @EnableSecurity

Where should application.properties live?

  1. Project root
  2. src/main/java
  3. src/main/resources
  4. target/