Spring Framework 6 Spring Boot 3 Notes Part1 _ Hyder Abbas
Spring Framework 6 Spring Boot 3 Notes Part1 _ Hyder Abbas
● Java Database Connectivity (JDBC): A set of APIs that allow Java programs to
interact with databases.
s
● Servlets: Java programs that run on a web server to handle HTTP requests and
responses.
ba
● JSP (Java Server Pages): Used to create dynamic web content by embedding Java
code in HTML.
Spring:
● Spring manages objects for you. Based on the configuration provided, it creates
objects, injects dependencies, and performs object lifecycle management.
Hyder Abbas
● Spring provides a comprehensive set of tools and best practices that make it easier to
develop Java applications.
● It simplifies the development process by managing infrastructure concerns (like
transactions, security, and database access), allowing developers to focus more on
business logic.
s
2. End-to-End Backend Development:
ba
● Spring can be used to develop the complete backend of an application, from handling
user requests in the web layer (Spring MVC) to managing data access (Spring Data),
and handling business logic in service layers.
● It is flexible enough to build monolithic applications or microservices-based architectures.
3. Versatility:
Ab
● Spring integrates easily with other technologies and frameworks, making it a versatile
choice for modern application development.
● For example, it integrates with Hibernate and JPA for data access, enabling seamless
database operations while abstracting complexity.
er
4. Object Management:
● Spring manages how objects are created and used, reducing the complexity of your
code by handling object lifecycle and dependency management.
yd
● With Dependency Injection (DI), Spring helps in decoupling components, making the
codebase more modular and maintainable.
● Spring allows you to inject dependencies (pass objects into other objects), which
makes your code flexible, loosely coupled, and easier to test.
● DI promotes better separation of concerns and easier unit testing of individual
components.
6. Modular Structure:
● Spring is organized into a wide array of modules (e.g., Spring MVC for web
applications, Spring Data for data access, Spring Security for authentication and
authorization, etc.).
Spring makes your code more testable, maintainable, and loosely coupled.
Spring Core:
● The Spring Core module is the foundation of all other modules and provides
s
fundamental features such as Dependency Injection, Inversion of Control (IoC),
and Bean management.
ba
● Dependency Management: The Spring Core module supplies the Spring container to
perform Dependency Management through Inversion of Control (IoC). This helps in
decoupling components
Ab
Dependency Management in Spring
target object by loading both classes and creating their instances. This is typically done using
Dependency Injection (DI), where one class (the dependent class) is injected into another (the
target class) to manage the relationships between objects.
yd
Setter Injection
● Setter Injection is the process of injecting dependencies into a target class through setter
methods.
● The Spring container first creates an instance of the target class, and then calls the
appropriate setter method to inject the dependent object.
Constructor Injection
● Constructor Injection involves passing the dependent object to the target object via the
constructor.
● This ensures that all required dependencies are provided at the time of object creation,
making the object immutable and fully initialized.
Field Injection
● Field Injection involves directly injecting dependencies into the fields of the target class
using reflection.
● The Spring container automatically assigns the dependency to the field at runtime.
s
ba
Spring Containers (IoC Containers)
Summary of Differences:
er
s
For most Spring applications today, ApplicationContext is recommended due
ba
to its richer feature set and better alignment with modern development practices.
Hyder Abbas
Spring Boot:
Spring Boot is one of the most efficient and simplified approaches to working with the
Spring Framework, offering a streamlined way to build Spring-based applications with
minimal configuration.
s
● Spring (Core framework)
● XML configurations (reduced or eliminated)
ba
● Auto Configuration (automatic setup based on context)
● Embedded Servers (no need to configure an external server)
● Actuators (for monitoring and management)
Ab
Key Features of Spring Boot:
on infrastructure setup.
or Netty.
● Distributed Applications: Develop distributed systems or web services, including
RESTful APIs, to interact with other applications.
2. Auto Configuration:
Spring Boot takes care of most of the configuration automatically based on the context of
the application.
s
● Database Connection Pooling: Spring Boot automatically configures database
ba
connections, removing the need for manual setup.
● Embedded Server Deployment: Spring Boot can deploy the web application in an
embedded server, eliminating the need for an external web server like Tomcat.
● IOC Container Management: The Spring Boot application will start the Inversion of
Control (IoC) container and manage your beans automatically.
Ab
● Component Scanning: Spring Boot will scan for components, services, and repositories
based on the configuration, reducing the need for manual bean registration.
3. Actuators:
Spring Boot includes Actuators that help you monitor and manage your application.
These are essential for maintaining application health and performance.
er
● Bean Count: See how many beans are loaded in your application.
● URL Mappings: View all URL patterns mapped in the application.
● Loaded Configuration Properties: List all properties that are loaded in the application.
yd
● Application Health: Check the health status of your application, including custom health
checks.
● Heap Dump: Get a snapshot of the JVM memory.
● Thread Dump: Inspect the application threads for performance issues.
H
4. Embedded Servers:
Spring Boot comes with built-in support for various embedded web servers, eliminating
the need for deploying your application to an external server.
● Apache Tomcat (default): Spring Boot comes pre-configured with Tomcat as the default
embedded server.
● Jetty: Another lightweight and fast server option.
● Netty: A non-blocking I/O server designed for high-performance applications.
Creating a Spring Boot Application:
You can create a Spring Boot application by visiting Spring Initializr, where you can generate a
custom Spring Boot project with your desired dependencies and configuration.
Many IDEs, such as IntelliJ IDEA and Eclipse , STS, provide direct integration with the Spring
Initializr website, allowing you to create and configure Spring Boot applications seamlessly.
s
Note: IDEs internally connect to start.spring.io to generate the Spring Boot project
ba
(Spring Starter Project).
A Spring Bean is any Java class (whether predefined, user-defined, or from a third-party
yd
library) whose object is created, managed, and maintained by the Spring container. Spring
Beans are at the heart of Spring’s IoC mechanism and undergo lifecycle management, including
dependency injection.
H
Stereotype Annotations:
These annotations help in defining the roles or responsibilities of the beans within the Spring
container. They are used to mark a class as a special type of Spring bean.
@Component
● Purpose: Marks a class as a Spring bean, indicating that it will be auto-detected through
classpath scanning and instantiated in the Spring container.
s
● Usage: Use @Component when you want to create a generic Spring bean.
@Service
ba
● Purpose: A specialization of @Component, used to annotate service-layer beans. It
provides better readability and indicates that the class is holding business logic.
● Usage: Use @Service to mark service classes that contain business logic or
service-related operations.
Ab
@Repository
@Controller
@RestController
● Purpose: Indicates that a class is a source of bean definitions. This annotation is used
for classes that define bean methods annotated with @Bean.
● Usage: Use @Configuration when you want to declare beans programmatically in
Java (instead of XML).
These annotations are used to inject dependencies into Spring beans, supporting Spring’s
Inversion of Control (IoC) container.
s
@Autowired
● Purpose: Automatically injects the dependency into a Spring bean, either by constructor,
ba
setter method, or field. Spring uses the @Autowired annotation to wire dependencies
automatically.
● Usage: Use @Autowired to inject dependencies into Spring beans.
@Qualifier
Ab
● Purpose: This annotation is used in conjunction with @Autowired when multiple beans
of the same type are available. It helps in specifying which bean should be injected when
there are multiple candidates.
● Usage: Use @Qualifier to specify the exact bean to inject.
er
@Primary
● Purpose: Specifies that a bean is the default choice for autowiring when multiple
candidates of the same type are present.
yd
● Usage: Use @Primary to mark a preferred bean when multiple beans are available for
autowiring.
@Bean
● Purpose: Indicates that a method produces a bean that should be managed by the
Spring container.
● Usage: Use @Bean within a class annotated with @Configuration to define a bean.
@PostConstruct and @PreDestroy
@Profile
● Purpose: Specifies that a bean is only available in certain environments or profiles (e.g.,
"development", "production").
s
● Usage: Use @Profile to define beans that should be loaded only in specific profiles.
ba
Hyder Abbas Ab
er
yd
H