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 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
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.
controller: This package contains the application's REST controllers or MVC controllers.
service: This package contains service classes, which hold the business logic.
model (or domain or entity): This package contains the application's data models, JPA entities, or any other data-related classes.
repository: This package contains repository or DAO classes, often interfaces when using Spring Data JPA, which interact with the database.
configuration: This package contains configuration classes, where you can define beans or any other Spring-related configurations.
resources/static: This directory contains static resources such as CSS, JavaScript, and images. They are served from the root path (/
).
resources/templates: This directory contains templates that will be processed by template engines like Thymeleaf.
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.
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.
Organizing packages in a Spring Boot application:
com.example.myapp ������ controller ������ service ������ repository ������ model ������ config
Layered architecture in Spring Boot:
com.example.myapp ������ controller �� ������ HomeController.java ������ service �� ������ MyService.java ������ repository �� ������ MyRepository.java ������ model ������ MyEntity.java
Separation of concerns in Spring Boot code structure:
com.example.myapp ������ web �� ������ controller �� ������ dto ������ service ������ repository ������ model
Common project structure conventions in Spring Boot:
src/main/java
for source code.myapp ������ src �� ������ main �� ������ java �� ������ com �� ������ example �� ������ myapp �� ������ controller �� ������ service �� ������ repository �� ������ model
Configuring multiple modules in Spring Boot project:
myapp ������ module1 �� ������ src �� ������ main �� ������ java ������ module2 �� ������ src �� ������ main �� ������ java ������ module3 ������ src ������ main ������ java
Using component scanning in Spring Boot:
// Enable component scanning @SpringBootApplication @ComponentScan(basePackages = "com.example.myapp") public class MyApplication { // Application code }
Structuring RESTful services in Spring Boot:
com.example.myapp ������ controller �� ������ UserController.java �� ������ ProductController.java ������ service ������ repository ������ model
Optimizing code structure for testing in Spring Boot:
com.example.myapp ������ main �� ������ controller �� ������ service �� ������ repository �� ������ model ������ test ������ controller ������ service ������ repository ������ model
Externalizing configuration and properties in Spring Boot:
application.properties
or application.yml
.application.properties
):# application.properties server.port=8080