# Project Structure — Spring Boot

Source: https://www.skillbyai.com/en/spring-boot/p-structure

> Package by feature.

## Feature packages and thin layers

Organise packages by **feature** (`products`, `orders`, `payments`), each containing its controller, service, repository and DTOs, rather than global `controllers` and `services` packages. Keep controllers thin (HTTP concerns only), put business rules in services, and keep the `@SpringBootApplication` class at the root and minimal. Spring Modulith can verify module boundaries and document dependencies as the code grows.

## Build services that last

Organise code by feature, keep up with Spring Boot releases, and review with a checklist.

![Three ideas: project structure, upgrading, checklist.](assets/figures/spring-boot/section-8-map.svg) — Figure 8.1 — Structure, upgrades and checklist.

## A feature-oriented layout

Names are examples.

```text
com.example.shop/
  ShopApplication.java          minimal main class
  products/
    ProductController.java      HTTP + DTOs
    ProductService.java         business rules, transactions
    ProductRepository.java      data access
    Product.java                entity
  orders/ ...
  config/                       security, properties, infrastructure beans
```

## Keep the main class minimal

As the testing pitfall showed, beans on the main class leak into every test slice.

**Quiz:** What does package-by-feature mean?

- [x] Grouping each feature's controller, service and repository together
- [ ] One package per annotation type
- [ ] Putting all code in the main class
- [ ] A package per developer

*Answer:* Grouping each feature's controller, service and repository together. Cohesive modules.
