|
@Component → Helps the IoC container recognize that a bean for this class needs to be created.
@SpringBootApplication → Combines the functionality of the following three annotations:
-
@ComponentScan → Makes the IoC container scan the current package for @Component. This means that if you create a class outside of this package and annotate it with @Component, it won’t be recognized unless that package is explicitly scanned.
-
@EnableAutoConfiguration → Enables Spring Boot to automatically configure various components. For example, if you provide port, username, and password details for a database in application.properties, Spring Boot will handle the rest.
-
@Configuration → Allows you to define beans within the class.
@RestController → A combination of @Controller and @ResponseBody.
Declares that the class is a controller which returns domain objects instead of views.
@RequestMapping → Maps HTTP requests to handler methods at the class level.
@GetMapping, @PostMapping, etc. → Map specific HTTP methods at the method level.
NOTE: Make sure the methods are declared public so that Spring can invoke them.
@RequestBody → Instructs Spring Boot to extract the request body and convert it into a Java object, which you can then access in code.
@Autowired → Instructs Spring Boot to inject the object/bean of this class.
ORM (Object-Relational Mapping) is a framework that maps Java objects to database tables.
It allows developers to manipulate data using Java objects.
JPA (Java Persistence API) helps implement ORM. It is a set of rules or interfaces that requires a persistence provider (ORM tool) to function.
Examples of persistence providers: Hibernate, EclipseLink, OpenJPA.
These providers implement the JPA specification and provide the actual functionality to interact with the database.
Spring Data JPA is built on top of JPA. It provides high-level abstractions and utilities that make working with JPA easier.
NOTE: JPA only works with relational databases, where the schema is rigid and tables are involved (e.g., MySQL).
It cannot be used with NoSQL databases like MongoDB, where schemas are flexible and data is stored in collections.
@Transactional → This tells spring where to apply transactional behaviour. i.e. in which method. It helps implement atomicity and
isolation in the service layer itself and gives control over it to developer.
@EnableTransactionManagement → It tells Spring to look for @Transactional annotations in code and apply transactional behavior to methods in Spring-managed beans. One needs to give the transaction manager for managing a transaction, this can be provided in application calls or as configuration.
Query Method DSL and Criteria API are two ways to interact with databases using Spring Data JPA and Spring Data MongoDB.
-
Query Method DSL → Allows you to define queries using method naming conventions.
-
Criteria API → Offers a dynamic and programmatic approach to write complex queries.
@Document → Indicates that the class is mapped to a MongoDB document. we can provide the collection name in it.
Typical Flow of Control in a Spring Boot Application
- A request comes to a controller whose
@RequestMapping matches the request’s URL prefix.
- It then gets transferred to the respective method via method-level mapping.
- The method calls the respective service method, which is responsible for performing all necessary operations before and after interacting with the database.
- When DB CRUD is needed, the service calls the repository.
- A repository is generally just an interface extending an existing repository interface.
- The methods for CRUD are already implemented in the JPA repository.
- Once the DB interaction is complete, the service performs post-DB tasks.
- Finally, the result is returned to the controller.
- The controller then returns the response in the required format with appropriate status codes, headers, etc.
flowchart TD
A[Incoming HTTP Request] --> B[Controller: @RequestMapping]
B --> C[Controller Method]
C --> D[Service Method]
D --> E{Need DB CRUD?}
E -- Yes --> F[Repository Interface]
F --> G[JPA Repository Implementation]
G --> H[Database]
H --> I[Return DB Result to Repository]
I --> J[Repository returns to Service]
D --> K[Post-DB Operations]
K --> L[Return Result to Controller]
L --> M[Controller prepares HTTP Response]
M --> N[Send Response to Client]
E -- No --> K
|