Java-DUO.pdf.crmgft
Java-DUO.pdf.crmgft
Spring Framework provides powerful features like Inversion of Control (IoC) and
Dependency Injection (DI).
IoC: Spring controls object lifecycles and dependencies.
DI: Spring wires objects together automatically, reducing tight coupling.
IoC transfers control of object creation and management from the developer to the Spring
Container.
Benefits:
Improves modularity.
Enhances testability.
Simplifies object lifecycle management.
@Autowired
public ServiceExample(RepositoryExample repository) {
this.repository = repository; } }
Tutor Mentor | 2024
11
Introduction to Dependency Injection (DI)
1.Constructor Injection:
@Component
public class MyService {
private final MyRepository repository;
@Autowired
public MyService(MyRepository repository) {
this.repository = repository;
}
}
Tutor Mentor | 2024
14
2. Setter Injection:
@Component
public class MyService {
private MyRepository repository;
@Autowired
public void setRepository(MyRepository repository) {
this.repository = repository;
}
}
Tutor Mentor | 2024
15
@Autowired
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
Tutor Mentor | 2024
17
Introduction to Spring’s Layered Architecture
Spring applications are organized into three layers:
1. Controller Layer: Handles HTTP requests.
2. Service Layer: Processes business logic.
3. Repository Layer: Interacts with the database.
THANK YOU
Ask Me Anything!