Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - Code Structure

Spring Boot does not enforce a specific project structure, but it does provide a set of conventions and best practices to organize your application code and resources effectively. Following these conventions makes it easier for developers familiar with Spring Boot to understand and work on the project.

Here's a typical project structure for a Spring Boot application:

myproject
│
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── myproject
│   │   │               ├── MyprojectApplication.java
│   │   │               ├── controller
│   │   │               ├── service
│   │   │               ├── model (or domain, entity)
│   │   │               ├── repository
│   │   │               └── configuration
│   │   │
│   │   ├── resources
│   │   │   ├── static
│   │   │   ├── templates
│   │   │   ├── application.properties (or application.yml)
│   │   │   └── ...
│   │
│   └── test
│       └── java
│           └── ...
│
├── .gitignore
├── pom.xml (for Maven) or build.gradle (for Gradle)
└── README.md

Breakdown:

  1. MyprojectApplication.java: This is the main entry point of the Spring Boot application. It typically contains the @SpringBootApplication annotation and the main method to bootstrap the application.

  2. controller: This package contains the application's REST controllers or MVC controllers.

  3. service: This package contains service classes, which hold the business logic.

  4. model (or domain or entity): This package contains the application's data models, JPA entities, or any other data-related classes.

  5. repository: This package contains repository or DAO classes, often interfaces when using Spring Data JPA, which interact with the database.

  6. configuration: This package contains configuration classes, where you can define beans or any other Spring-related configurations.

  7. resources/static: This directory contains static resources such as CSS, JavaScript, and images. They are served from the root path (/).

  8. resources/templates: This directory contains templates that will be processed by template engines like Thymeleaf.

  9. resources/application.properties (or application.yml): This is the main configuration file for your Spring Boot application. You can define properties like the server port, database configurations, custom application properties, etc.

  10. test: This directory contains test classes for your application.

By following this structure, you're adhering to the "Convention over Configuration" principle that Spring Boot advocates. It's worth noting that while this structure is recommended, Spring Boot is quite flexible, and you can organize your project in the way that makes the most sense for your use case.

  1. Organizing packages in a Spring Boot application:

    • Organize packages based on functionality or modules.
    • Example:
      com.example.myapp
      ������ controller
      ������ service
      ������ repository
      ������ model
      ������ config
      
  2. Layered architecture in Spring Boot:

    • Adopt a layered architecture with clear separation of concerns.
    • Example:
      com.example.myapp
      ������ controller
      ��   ������ HomeController.java
      ������ service
      ��   ������ MyService.java
      ������ repository
      ��   ������ MyRepository.java
      ������ model
          ������ MyEntity.java
      
  3. Separation of concerns in Spring Boot code structure:

    • Separate concerns like presentation, business logic, and data access.
    • Example:
      com.example.myapp
      ������ web
      ��   ������ controller
      ��   ������ dto
      ������ service
      ������ repository
      ������ model
      
  4. Common project structure conventions in Spring Boot:

    • Follow common conventions like src/main/java for source code.
    • Example:
      myapp
      ������ src
      ��   ������ main
      ��       ������ java
      ��           ������ com
      ��               ������ example
      ��                   ������ myapp
      ��                       ������ controller
      ��                       ������ service
      ��                       ������ repository
      ��                       ������ model
      
  5. Configuring multiple modules in Spring Boot project:

    • Use a multi-module project for large applications.
    • Example:
      myapp
      ������ module1
      ��   ������ src
      ��       ������ main
      ��           ������ java
      ������ module2
      ��   ������ src
      ��       ������ main
      ��           ������ java
      ������ module3
          ������ src
              ������ main
                  ������ java
      
  6. Using component scanning in Spring Boot:

    • Leverage component scanning to automatically detect and register Spring beans.
    • Example:
      // Enable component scanning
      @SpringBootApplication
      @ComponentScan(basePackages = "com.example.myapp")
      public class MyApplication {
          // Application code
      }
      
  7. Structuring RESTful services in Spring Boot:

    • Organize RESTful services based on resources.
    • Example:
      com.example.myapp
      ������ controller
      ��   ������ UserController.java
      ��   ������ ProductController.java
      ������ service
      ������ repository
      ������ model
      
  8. Optimizing code structure for testing in Spring Boot:

    • Separate production and test code with clear testing packages.
    • Example:
      com.example.myapp
      ������ main
      ��   ������ controller
      ��   ������ service
      ��   ������ repository
      ��   ������ model
      ������ test
          ������ controller
          ������ service
          ������ repository
          ������ model
      
  9. Externalizing configuration and properties in Spring Boot:

    • Externalize configuration using application.properties or application.yml.
    • Example (application.properties):
      # application.properties
      server.port=8080