SteelReed

Search

Search posts, questions…

Spring BootQuestion 1 / 26

Junior

What is the difference between IoC and Dependency Injection? What are a bean and the ApplicationContext?

Short answer

IoC is the principle: your code no longer creates its dependencies, the framework does. DI is how you get IoC: dependencies are passed in; ApplicationContext is the container that creates and manages the beans.

createscreatesinjected via constructornew with mocksApplicationContextOrderRepository beanOrderService beanUnit test

3 points to say:

  1. IoC is the principle, DI is the technique: dependencies passed in via constructor, setter or field.
  2. A bean is an object Spring manages; ApplicationContext is the container: eager singletons, events, profiles, i18n.
  3. The real benefit of DI: tests without a container, and a domain that stays independent of the framework.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.