Sobes.tech
Junior — Middle

What dependency injection approach is used in Spring Framework?

sobes.tech AI

Answer from AI

The Spring Framework uses dependency injection through Inversion of Control (IoC) with the help of the Spring container.

Main methods of dependency injection:

  • Through constructor — dependencies are passed via constructor parameters.
  • Through setters — dependencies are set via setter methods.
  • Through fields (field injection) — dependencies are injected directly into fields using annotations.

Spring manages the lifecycle of objects and their dependencies, creating and linking beans in the container. Annotations like @Autowired are used to define dependencies for automatic wiring.

Example of constructor injection:

@Component
public class Service {
    private final Repository repo;

    @Autowired
    public Service(Repository repo) {
        this.repo = repo;
    }
}

Thus, Spring implements dependency injection through the IoC container, which simplifies dependency management and enhances code testability.

What dependency injection approach is used in Spring… - sobes.tech