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

    Project Structure

    A Spring Boot Maven project follows the standard layout: application code under src/main/java, resources under src/main/resources, tests mirroring main under src/test.

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

    Introduction

    A Spring Boot Maven project follows the standard layout: application code under src/main/java, resources under src/main/resources, tests mirroring main under src/test. Tooling, IDEs, and Spring Boot's default component scan assume this shape.

    Place OrdersApplication in com.techlearningpro.orders. Component scan starts at that package and walks downward. Controllers, services, and config that live in a sibling package outside that root will not become beans unless you widen scanBasePackages on purpose.

    Early Orders can use layer packages (controller, service, repository). As the service grows, feature packages (pricing, fulfillment) often reduce merge pain — the dependency rule from the architecture lesson does not change.

    Configuration belongs in application.yml (this course's default). Flyway/Liquibase scripts later land under db/migration. Never commit target/.

    Purpose of this lesson

    Navigate the Orders Maven tree, place the application class correctly for component scan, and choose a package layout that will not fight Boot defaults.

    Understanding the topic

    Structure is a contract with the build and with Spring.

    • Entry class: @SpringBootApplication at the root package of the service.
    • resources: YAML, static assets, migration scripts — not Java.
    • tests: same package names as main so package-private test tricks stay available.
    • build output: target/ is disposable; CI recreates it.
    • wrappers: mvnw lives at the repo root beside the POM.

    Step-by-step explanation

    1. Open the unzipped Orders project and locate OrdersApplication.java.
    2. Confirm its package is the ancestor of your controller package.
    3. Add empty controller, service, and dto packages under that root.
    4. Run ./mvnw -q test after any package move to catch scan mistakes early.

    Syntax reference

    Reference tree for Fundamentals:

    bash
    orders/
    ├── pom.xml
    ├── mvnw / mvnw.cmd
    ├── src/main/java/com/techlearningpro/orders/
    │ ├── OrdersApplication.java
    │ ├── controller/
    │ ├── service/
    │ ├── dto/
    │ └── config/
    ├── src/main/resources/
    │ └── application.yml
    └── src/test/java/com/techlearningpro/orders/

    Real-world use

    When a new engineer joins TechLearningPRO checkout, they expect the application class at the scan root and YAML under resources. Surprises — business logic in config, or controllers in com.techlearningpro.api outside the scan — cost hours of 'why is everything 404'.

    Best practices

    • Keep the application class at the service root package.
    • Mirror main packages in test.
    • Switch to feature packages when layer folders become dump grounds.

    Common mistakes

    • Moving OrdersApplication to a deeper package and leaving controllers above it.
    • Committing IDE files and target/.
    • Putting secrets in src/main/resources because 'it is not code'.

    Advanced interview questions

    Interview Prep

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

    3 questions
    1BeginnerQuestionWhere should @SpringBootApplication live?+

    Answer

    In the root package of the application so default component scanning finds controllers, services, and configuration beneath it.
    2IntermediateQuestionWhat happens if a @RestController sits outside the scan base package?+

    Answer

    The application starts, but the controller is not a bean, so routes are missing and clients see 404.
    3AdvancedQuestionPackage-by-layer vs package-by-feature — how do you choose?+

    Answer

    Layer packages are fine for small services. Feature packages scale better for larger codebases and team ownership. Keep dependencies pointing inward the same way either way.

    Hands-on exercise

    Lab. Draw your Orders tree. Mark the scan root with a star. Intentionally move a future controller package outside it on paper and write what symptom you would see at runtime (empty mappings / 404 while the process is healthy).

    Summary

    Orders is a standard Maven tree with a deliberate scan root. Next you write the first controller and prove the process serves JSON.

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