1. What is IoC (Inversion of Control) or Dependency Injection?
Inversion of Control (IoC) is a design principle where control of object creation and binding is shifted from the application code to a container/framework. In Dependency Injection (DI), this is done by externally injecting dependencies (objects) into a class rather than the class creating them itself.
-
Example: Instead of
new Service()
, you declare aService
field and let the Spring container inject the instance. -
Benefits:
-
Decouples class dependencies.
-
Improves maintainability and testability.
✅ What is Dependency Injection (DI)?
Dependency Injection is a specific implementation technique of IoC. It is a pattern where dependencies (objects that a class needs to perform its work) are "injected" into the class, instead of the class creating them directly.
Think of IoC as the general concept, and DI as one of the ways to achieve it.
✅ Types of Dependency Injection in Spring:
-
Constructor Injection
-
Setter Injection
-
Field Injection (not recommended for testing)
✅ Example Without DI (Tight Coupling):
-
The
StudentService
class creates theStudentRepository
on its own usingnew
. -
Hard to test and maintain.
✅ Same Example With DI (Loose Coupling using IoC):
Step 1: Define the Repository
Step 2: Inject the Repository into the Service
-
Here,
StudentRepository
is injected by the Spring IoC container. -
The class does not manage the creation of its dependency.
✅ Spring Boot Main Class
✅2. Types of Dependency Injection
Spring supports:
-
Constructor Injection
-
Setter Injection
Other frameworks (like Avalon) also support Interface Injection, but Spring does not.
a. Constructor Injection
b. Setter Injection
3. Benefits of IoC (Dependency Injection)
-
Less boilerplate code: We don’t need to write factory or singleton code to manage dependencies.
-
Improved testability: Dependencies can be easily mocked.
-
Loose Coupling: Components are independent of how their dependencies are constructed.
-
Lifecycle and scope control: Spring manages object scopes (singleton, prototype, etc.).
-
Support for AOP: Cross-cutting concerns like logging, security can be injected.
4. What is Spring Framework?
Spring is a lightweight, open-source Java framework for building enterprise-grade applications. It provides a comprehensive infrastructure support for developing Java applications.
Advantages:
-
Modular & Layered Architecture.
-
Promotes Plain Old Java Object (POJO) programming.
-
Reduces boilerplate code through IoC/DI.
-
Open-source and vendor-neutral.
5. Features of Spring
Feature | Description |
---|---|
Lightweight | Minimal memory and processing overhead. |
IoC | Dependencies are injected into components. |
AOP (Aspect-Oriented Programming) | Separates cross-cutting concerns like logging/security. |
Container | Manages object lifecycle and configuration. |
Spring MVC | Powerful and extensible web MVC framework. |
Transaction Management | Abstracts transactions across databases and APIs. |
JDBC Exception Handling | Simplifies JDBC error handling via custom exception hierarchy. |
ORM Integration | Seamlessly integrates with Hibernate, JPA, etc. |
6. Spring MVC Example
7. JDBC Exception Handling
Spring provides a cleaner way of dealing with exceptions:
8. Types of Dependency Injection Spring Supports
-
Constructor Injection: Preferred when dependencies are mandatory.
-
Setter Injection: Preferred when dependencies are optional.
Example of Constructor Injection:
Example of Setter Injection:
9. What is BeanFactory?
BeanFactory is the simplest Spring IoC container. It lazily loads beans and manages their lifecycle.
Core Interfaces:
-
BeanFactory
is the root interface. -
Implemented by classes like
XmlBeanFactory
.
Why prefer ApplicationContext?
-
Supports internationalization (i18n)
-
Publishes events to listeners
-
Resolves file resources
-
Autowires annotations like
@Autowired
,@Component
Example:
No comments:
Post a Comment