Foundation to Spring Boot Beginners
Spring Boot is a lightweight, powerful framework, and built on top of the Spring Framework.
Spring Boot module provides a Rapid Application Development feature to the Spring framework.
📝Key points we should focus on learning about the Spring Boot,
1. Spring Initializer (start.spring.io)
- Web-based tool
- Bootstrap Spring Boot projects
- Allows to select relevant dependencies, packaging options, and project settings
- Generate a customized project structure
2. Dependencies
- Use as external modules/plugins to enhance the functionality of the project
3. Dependency Injection
- A core concept in Spring Boot
- Facilitate loose coupling by allowing management and inject dependencies to application components
- Enhance the flexibility and maintainability
4. Maven/ Gradle
- Spring Boot projects can be built using either Maven or Gradle
- Easy to manage dependencies efficiently
- Automatically fetch required libraries
- Maintain the consistency of the project builds
5. Embedded HTTP Servers
- Spring Boot has embedded HTTP servers like Tomcat
- Eliminate the use of external server configurations
- Simplify the deployment
6. JPA
- Java Persistence API
- Stands for ORM(Object Relational Mapping) in Java applications
- Spring Boot provides easy integration with JPA for data access
- Streamline database operations
- Interact with database
7. Annotations
- Use to simplify the configurations
- Reducing boilerplate codes
- Enhance the readability
8. Jar
- Spring Boot applications are usually packaged as JAR(Java Archive) files
- Standalone executable JAR simplifies deployment and execution
9. Lombok Library
- Lombok is a Java library that helps reduce boilerplate code by automatically generating common code blocks such as getters, setters, and, constructors.
- So, Lombok provides annotations. (We will discuss the annotations belonging to this Lombok library in the latter part of this article).
The flow of Spring Boot Application
💎FrontEnd
A client that is responsible for interacting with the user and recent data
💎Backend
- Controller
- Serving as an entry point for processing user inputs.
- The controller can handle requests and responses.
- Controller-received things are attached to a DTO and sent to the service layer.
2. API(Application Programming Interface)
- RESTful APIs that allow communication between the frontend and backend.
3. DTO(Data Transferring Objects)
- Objects used to transfer data between different layers of an application, typically between the frontend, controller, and service layers.
4. Service
- Contain business logic and perform operations requested by controllers.
5. Entity
- Represent objects stored in DB.
- Most of the time entity will map the table in the DB
6 . Repository
- Provide an abstraction for data access
- Interact with databases to handle CRUD operations
7. Database
- Stores and manages data persistently
- Spring Boot supports various databases(MongoDB, MySQL, MSSQL, PostgreSQL)
- Can use technologies like JPA to interact with databases easily
🔮Annotations
@Entity
- When the application runs, Hibernate, the JPA provider, creates a table in the database based on this entity.
@Id
- Use to designate a field as the primary key of an entity
- Signifies the unique identifier for database records
@Query
- Used to declare custom queries in Spring Data JPA
- Allows developers to define complex database operations(custom inserts or updates)
@RestController
(response body + controller)
- Responses are converted to XML/JSON automatically
@RequestMapping
- Maps HTTP requests to handler methods in a controller
@GetMapping — Handle HTTP GET requests, and use for retrieving resources or data
@PostMapping — Handle HTTP POST requests, and used for creating or adding new resources
@PutMapping — Handle HTTP PUT requests, and used for updating existing resources or creating if not present
@DeleteMapping — Handle HTTP DELETE requests, and used for for deleting or removing resources
@CrossOrigin
- Cross-origin resource sharing when the frontend and backend are in two origins (It is blocked for a security reason)
@Autowired
- Used for automatic dependency injection (If two things depend mostly on each other then as a dependency we can inject that into another class)
@Service
- Identify the service layer when performing business logic or service-layer operations
@Transactional
- Indicates that a method, or all methods in a class, should be executed within a database transaction.
- Ensures consistency for database operations, preventing data inconsistencies.
@AllArgsConstructor/@NonArgsConstructor
- These 2 annotations are provided by the Lombok Library.
@AllArgsConstructor
generate a constructor with arguments for all fields in the class.@NoArgsConstructor
generates a constructor with no arguments.
@Builder
- Provided by the Lombok Library.
- Generates a builder pattern for class.
@Getter / @Setter
- Provided by the Lombok Library.
- Generates getter and setter methods for the fields in the class.
@ToString
- Provided by the Lombok Library.
- Generates a
toString()
method for the class, including all fields.
@Data
- Provided by the Lombok Library.
- This is a combination of
@Getter
,@Setter
,@ToString
,@EqualsAndHashCode
, and@RequiredArgsConstructor
annotations. - Generates common boilerplate code like getters, setters, toString, equals, hashCode, and constructors, streamlining Java class definitions.
In this article, I hope to make you aware of the background and the jargon you may know to learn Spring Boot. So, I will guide you to implement a simple Spring Boot application in the next blogs.
Stay tuned guys😎, until then bye byeeee…👋🏼👋🏼