Inversion of Control: ApplicationContext and BeanFactory

How the Spring IoC container works: BeanFactory vs ApplicationContext, bean definitions, the refresh() startup sequence, BeanFactoryPostProcessor and BeanPostProcessor extension points, dependency resolution (by type, @Qualifier, @Primary, collections, ObjectProvider), circular dependencies, context hierarchies, and AOT processing.

On this page

Simple definition#

Inversion of Control (IoC) means your objects do not create or look up their collaborators: a container creates them, wires them and manages their lifecycle. In Spring, that container is the ApplicationContext, built on the lower-level BeanFactory.

The problem#

In a large application, wiring hundreds of objects by hand is repetitive and error-prone: the correct order, singletons, configuration, proxies for transactions and security, shutdown. See Dependency Injection in Java for the plain-Java version. The container automates that composition root.

BeanFactory vs ApplicationContext#

BeanFactoryApplicationContext
RoleCore DI engine: definitions, creation, wiringFull container: a BeanFactory plus enterprise features
Singleton creationLazy (on first getBean)Eager at startup (fail fast)
Post-processorsMust be registered manuallyDetected and applied automatically
Events❌✅ ApplicationEventPublisher
i18n messages❌✅ MessageSource
Resources, environment, profilesBasic✅ ResourceLoader, Environment
UseInside frameworksAlways in applications

Spring Boot creates the right context for you: AnnotationConfigServletWebServerApplicationContext for MVC, the reactive equivalent for WebFlux, or a plain context for batch and CLI applications.

Bean definitions: recipes before objects#

A BeanDefinition describes how to create a bean: class or factory method, scope, constructor arguments, dependencies, init and destroy methods, lazy flag and primary flag. Definitions come from:

  • @Component-family classes found by component scanning
  • @Bean methods in @Configuration classes
  • auto-configuration (Spring Boot)
  • programmatic registration (BeanRegistrar in Spring 7, registerBean).

Startup: what refresh() does#

Open the interactive diagram: How the Spring ApplicationContext starts Standalone page with zoom, light/dark themes and SVG/PNG export, generated with Archify. Diagram source.

  1. Load bean definitions (scanning, configuration classes, auto-configuration imports).
  2. Run BeanFactoryPostProcessors. They modify definitions before any bean exists. Example: ConfigurationClassPostProcessor processes @Configuration, and property placeholders are resolved.
  3. Register BeanPostProcessors.
  4. Instantiate non-lazy singletons in dependency order. Inject dependencies, call Aware callbacks and init methods; BeanPostProcessors may wrap each bean, for example in AOP proxies for @Transactional.
  5. Publish ContextRefreshedEvent. Spring Boot then starts the web server, runs ApplicationRunners and CommandLineRunners, and publishes ApplicationReadyEvent.
Extension pointWorks onTypical use
BeanFactoryPostProcessor / BeanDefinitionRegistryPostProcessorDefinitionsRegister or modify definitions, process config classes
BeanPostProcessorBean instancesWrap in proxies (AOP), process annotations (@Autowired, @PostConstruct)
SmartInitializingSingletonAfter all singletons existPost-startup wiring
ApplicationListenerEventsReact to lifecycle events

Resolving dependencies#

Spring injects by type, then narrows down:

@Service
class CheckoutService {
    CheckoutService(PaymentGateway gateway,                      // single candidate, or @Primary
                    @Qualifier("fraudRules") List<Rule> rules,   // all beans of the type, ordered by @Order
                    ObjectProvider<AuditClient> audit) {         // optional or lazy lookup
        ...
    }
}
SituationResolution
Exactly one bean of the typeInjected
Several beans@Primary, @Qualifier, or the parameter name matching the bean name; otherwise NoUniqueBeanDefinitionException
NoneNoSuchBeanDefinitionException, unless the parameter is Optional<T>, ObjectProvider<T> or @Autowired(required=false)
List<T>, Map<String,T>All candidates (ordered by @Order or Ordered)
Generic typesRepository<Order> vs Repository<Customer> resolved by generic type

Circular dependencies#

A → B → A:

  • Constructor injection: fails (BeanCurrentlyInCreationException). That is good: it reveals a design problem.
  • Spring Boot 2.6+ forbids circular references by default, even with setter or field injection.
  • Fixes: extract a third component, invert one dependency with an event or interface, or, as a last resort, use @Lazy on one injection point.

Context hierarchies#

A child context can see beans in its parent, but not the reverse. This appears in classic Spring MVC (root plus servlet context), in Spring Cloud (bootstrap and child contexts), and in tests. Most Spring Boot apps use a single context.

AOT and native images#

Spring’s Ahead-of-Time (AOT) engine runs the bean-definition phase at build time and generates Java code for the definitions, plus reflection hints. Benefits: faster startup, GraalVM native images, and smaller memory with CDS / Project Leyden AOT caches. The trade-off: conditions are evaluated at build time, so the bean graph must be fixed.

Common mistakes#

Quick check#

BeanFactory vs ApplicationContext? BeanFactory is the core DI engine. ApplicationContext adds eager singletons, automatic post-processor registration, events, i18n, resources and environment, and is what applications use.

BeanFactoryPostProcessor vs BeanPostProcessor? The first modifies bean definitions before any bean is created. The second processes bean instances after creation, for example by wrapping them in proxies.

How does Spring choose among several beans of the same type? @Primary, @Qualifier, or the parameter name; or you inject all of them as a List or Map.

What happens with constructor-injection cycles? Startup fails with BeanCurrentlyInCreationException. Redesign, or use @Lazy as a last resort.

What does Spring AOT do? It processes bean definitions at build time and generates code and hints for faster startup and GraalVM native images.

Summary#

  • The ApplicationContext is the IoC container: definitions first, then instances.
  • Extension points: BeanFactoryPostProcessors for definitions, BeanPostProcessors for instances (proxies).
  • Inject by type and narrow with @Primary and @Qualifier; use ObjectProvider for optional or lazy dependencies.
  • Avoid cycles; prefer constructor injection; AOT moves work to build time.

References#