Sobes.tech
Junior — Middle

How can dependencies from the context be injected into your class using binding?

sobes.tech AI

Answer from AI

Dependency injection from the context using binding is usually implemented through Dependency Injection (DI) containers, such as in the Spring Framework. Binding is the association of an interface with a specific implementation or object in the application context.

Main methods of dependency injection:

  • Through constructor — dependencies are passed via constructor parameters.
  • Through setters (setter injection) — dependencies are set via setter methods.
  • Through fields (field injection) — dependencies are injected directly into class fields (for example, using the @Autowired annotation in Spring).

Example of binding and dependency injection in Spring:

@Component
public class Service {
    // service implementation
}

@Component
public class Client {
    private final Service service;

    @Autowired
    public Client(Service service) {
        this.service = service; // injection via constructor
    }
}

In this example, Spring automatically binds the Service interface or class to its implementation and injects an instance into the Client class.

How can dependencies from the context be injected… - sobes.tech