Category: java
spring boot IOC
Published on 06 Apr 2026
Explanation
IoC (Inversion of Control) is a design
principle where the control of object
creation and dependency management
is transferred from the
programmer to the Spring framework.
Instead of manually creating objects,
Spring automatically manages them.
Code:
// Without IoC (manual object creation) StudentService service = new StudentService();
Explanation
With IoC in Spring Boot, objects are
created and managed by the Spring Container
automatically using annotations
like @Component, @Service, @Repository,
and @Controller.
Code:
@Service
public class StudentService {
}
Explanation
Spring Container is responsible for
creating objects
(beans), managing their lifecycle,
and injecting dependencies
wherever required.
Code:
@Component
public class CourseService {
}
Explanation
Dependency Injection (DI) is the
implementation of IoC in Spring Boot.
Dependencies are injected
automatically instead of being
manually created.
Code:
@Service
public class StudentService {
private CourseService courseService;
public StudentService(
CourseService courseService) {
this.courseService = courseService;
}
}
Explanation
@Autowired annotation is commonly used to
inject dependencies automatically from
the Spring Container.
Code:
@Service
public class StudentService {
@Autowired
private CourseService courseService;
}
Explanation
IoC improves code flexibility, reduces tight
coupling between classes, and makes
applications easier to maintain.
It is widely used in Spring Boot
applications such as REST APIs,
student systems,
and training platforms like the ones
you build π.
Code:
@RestController
public class StudentController {
private final StudentService studentService;
public StudentController(StudentService
studentService) {
this.studentService =
studentService;
}
}