Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 100

    Real Production Folder Structure

    Serious teams organise a Spring Boot 3 service by feature, then layer inside each feature.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Serious teams organise a Spring Boot 3 service by feature, then layer inside each feature. TechLearningPRO Orders grows painful when everything lives in controller/, service/, repository/ packages that sprawl across domains.

    Package-by-feature keeps orders, billing, and catalog changes local. Clean-architecture folders inside a feature (api, application, domain, infrastructure) keep HTTP, use cases, and persistence honest.

    This lesson shows a production layout you can paste into a Boot 3 / Java 21 Orders repo today.

    Purpose of this lesson

    Restructure Orders into feature packages with internal clean-architecture layers suitable for Boot 3 production teams.

    Understanding the topic

    Feature first; layers second; shared means truly shared.

    • Feature packages — orders/, billing/, catalog/.
    • Internal layers — api → application → domain → infrastructure.
    • config/ — Security, Kafka, Observability beans.
    • shared/ — money, ids, problem-details — not a dumping ground.
    • Tests — mirror the feature tree; Testcontainers at the feature edge.

    Step-by-step explanation

    1. Create com.techlearningpro.orders.orders with api/application/domain/infrastructure.
    2. Move controllers/DTOs into api, use cases into application, aggregates into domain.
    3. Keep JPA entities and Kafka publishers under infrastructure.
    4. Delete cross-feature imports that pierce domain boundaries; route via application services.

    Syntax reference

    Production Orders tree (package-by-feature).

    bash
    com.techlearningpro.orders/
    ├── OrdersApplication.java
    ├── config/
    │ ├── SecurityConfig.java
    │ ├── KafkaConfig.java
    │ └── ObservabilityConfig.java
    ├── orders/
    │ ├── api/
    │ │ ├── OrderController.java
    │ │ ├── dto/
    │ │ └── ApiExceptionHandler.java
    │ ├── application/
    │ │ ├── PlaceOrderUseCase.java
    │ │ ├── CancelOrderUseCase.java
    │ │ └── OrderQueries.java
    │ ├── domain/
    │ │ ├── Order.java
    │ │ ├── OrderItem.java
    │ │ └── events/OrderPlaced.java
    │ └── infrastructure/
    │ ├── persistence/
    │ │ ├── JpaOrderRepository.java
    │ │ └── OrderEntity.java
    │ └── messaging/
    │ └── OrderEventsPublisher.java
    ├── billing/
    │ └── ...
    └── shared/
    ├── ids/
    └── money/

    Real-world use

    TechLearningPRO's first Orders monolith used layer packages. A 'simple' refund change touched twelve packages and three merge conflicts. After packaging by feature, refund work stayed inside orders/ + billing/, review time dropped, and new engineers found the use case in minutes.

    Security implications

    Structure affects how easily security rules stay consistent.

    • Centralise HTTP security in config/SecurityConfig; features declare method security where needed.
    • Do not scatter hardcoded role strings — use shared constants.
    • Keep secret-loading adapters in infrastructure, never in domain.

    Best practices

    • One public API per feature module; hide infrastructure types.
    • Prefer package-private domain types; export use-case interfaces.
    • Keep config thin — no business rules in @Configuration classes.

    Common mistakes

    • A fat shared/ that becomes the new god package.
    • Putting JPA entities in domain/ and calling that DDD.
    • Circular feature dependencies that force a rebuild of everything.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionWhen should you switch from package-by-layer to package-by-feature?+

    Answer

    When multiple domains share the same repo and layer packages cause constant cross-cutting churn — often beyond ~10 controllers.
    2BeginnerQuestionWhat belongs in shared/?+

    Answer

    Truly cross-cutting primitives (money, ids, errors) — not feature services parked 'temporarily'.
    3IntermediateQuestionWhere do Kafka publishers live?+

    Answer

    In infrastructure adapters; domain emits events, application orchestrates, infrastructure publishes.

    Hands-on exercise

    Lab. Take a flat Orders sample and migrate place/cancel into the feature tree above. Prove with a package diagram and a failing ArchUnit test that domain does not import infrastructure.

    Summary

    Folder structure makes Orders navigable and reviewable. Next: practice modules turn this architecture into shipped exercises.

    Ready to mark this lesson complete?Track your journey across the entire course.