Suggest an editImprove this articleRefine the answer for “What does "container-based DI" mean?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Container-based DI** is an approach in which dependency management is handled by a dedicated container (an IoC container), not by the programmer: instead of manually creating and wiring objects together, you just describe which classes and dependencies are needed, and the container does the rest. **Key point:** the container itself creates, wires, and manages the lifecycle of objects, letting the developer focus on the logic rather than on who creates the dependencies and how.Shown above the full answer for quick recall.Answer (EN)Image**Container-based DI** is an approach in which **dependency management is handled by a dedicated container (an IoC container)**, not by the programmer. That is, instead of manually creating and wiring objects together, you just **describe which classes and dependencies are needed**, and the container does the rest. --- ### How it works 1. **You declare classes and dependencies** For example, in Spring or .NET Core: ```java @Component class UserService { private final Database db; public UserService(Database db) { this.db = db; } } @Component class MySQLDatabase implements Database { ... } ``` 2. **The container scans the application** It sees the annotations (`@Component`, `@Service`, `@Inject`, `@Autowired`, etc.) and creates objects for all the needed classes. 3. **The container resolves dependencies** It automatically figures out which implementation to plug in, for example, that `Database` = `MySQLDatabase`. 4. **The container manages the objects' lifecycle** It decides: - when to create the object (singleton, prototype, etc.), - who to hand it to, - when to destroy or reinitialize it. --- ### Example: without a container vs with a container **Without a container (manual DI):** ```java Database db = new MySQLDatabase(); UserService userService = new UserService(db); ``` **With container-based DI:** ```java ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserService userService = context.getBean(UserService.class); ``` The container itself: - creates `MySQLDatabase`, - figures out that `UserService` depends on it, - plugs it into the constructor, - hands back a ready-made object. --- ### Why this is needed **Loose coupling:** classes do not depend on specific implementations. **Modularity and scalability:** new components can be added easily. **Lifecycle management:** the container decides when and how to create objects. **Testing:** dependencies can be swapped for mock objects in configuration. **Unification:** the container sets a common way to configure and wire components across the whole system. --- ### Examples of DI containers - **Java:** Spring Framework, Guice, CDI (Contexts and Dependency Injection) - **.NET:** Microsoft Dependency Injection Container, Autofac, Ninject - **JavaScript/TypeScript:** InversifyJS, NestJS - **Python:** dependency-injector, FastAPI Depends --- **Conclusion:** > Container-based DI is when *a dedicated container takes over creating, wiring, and managing dependencies*. > You no longer create objects by hand, you just describe the relationships, and the container automatically builds and maintains the whole architecture.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.