R
Rishtaara
Spring Boot Fundamentals
Lesson 2 of 8Article18 minFREE

Spring Core and Dependency Injection

Inversion of Control means the container creates and wires objects for you. Constructor injection keeps dependencies explicit and testable.

IoC and DI

Inversion of Control means the container creates and wires objects for you. Constructor injection keeps dependencies explicit and testable.

Service with constructor injection
@Service
public class CourseService {
  private final CourseRepository courseRepository;

  public CourseService(CourseRepository courseRepository) {
    this.courseRepository = courseRepository;
  }

  public List<Course> listCourses() {
    return courseRepository.findAll();
  }
}