Interview Prep Zoneby CuriouserLabs

Question 1 of 5 · senior level

What is Inversion of Control, and what exactly happens when the ApplicationContext starts?

🎤 Say this first

IoC means the container constructs and wires objects; your classes declare needs and never new their collaborators. Startup has distinct phases: (1) scan/parse config into BeanDefinitions (recipes, no objects); (2) run BeanFactoryPostProcessors which may mutate recipes (property placeholders); (3) instantiate singletons in dependency order, injecting as it goes, passing each bean through the BeanPostProcessor pipeline (where AOP proxies appear); (4) publish refresh events, start SmartLifecycle beans. Knowing the phases is what distinguishes a senior answer.

The full picture

🎬 How the IoC container builds your app

1 / 6
Your classes 📄@Service OrderService@Repository OrderRepo@Configuration + @Beanscan 🔍ApplicationContext (IoC container) 🏭BeanDefinition: serviceBeanDefinition: repoDefinition phase → post-process phase → creation phase. Knowing the phases is what separates senior answers.

Startup: component scanning parses @Component/@Service/@Repository classes and @Bean methods in @Configuration classes.

Scan → definitions → post-process → create → inject → ready.
  • BeanDefinition = metadata (class, scope, constructor args, lazy flag) — the container's plan. @Component scanning, @Bean methods, and imports all funnel into the same registry.
  • Two post-processor families: BeanFactoryPostProcessor edits definitions before creation; BeanPostProcessor intercepts instances during creation. Mixing them up is a classic interview trap.
  • Eager by default: singletons are created at startup — failures surface at boot, not at 3am on first request. @Lazy trades boot time for runtime risk.
  • Benefits framing: testability (inject fakes), decoupled construction (composition root), lifecycle & scopes handled, and a single interception point for cross-cutting concerns.

🔄 Likely follow-up questions

  • ApplicationContext vs BeanFactory — what does the context add?
  • When would you register a BeanDefinition programmatically? (frameworks, dynamic feature wiring)
  • What happens if two beans of the same type exist and one is @Primary?