Sobes.tech
Junior — Middle

Millist sõltuvuste süstimise lähenemist kasutatakse Spring Frameworkis?

sobes.tech AI

Vastus AI-lt

In the Spring Framework, dependency injection is used through Inversion of Control (IoC) with the help of the Spring container.

The main methods of dependency injection are:

  • Through the constructor — dependencies are passed via constructor parameters.
  • Through setters — dependencies are set via setter methods.
  • Through 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 for automatic wiring.

Example of constructor injection:

@Component
public class Service {
    private final Repository repo;

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

This way, Spring implements dependency injection via the IoC container, simplifying dependency management and increasing code testability.