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#
BeanFactory | ApplicationContext | |
|---|---|---|
| Role | Core DI engine: definitions, creation, wiring | Full container: a BeanFactory plus enterprise features |
| Singleton creation | Lazy (on first getBean) | Eager at startup (fail fast) |
| Post-processors | Must be registered manually | Detected and applied automatically |
| Events | ❌ | ✅ ApplicationEventPublisher |
| i18n messages | ❌ | ✅ MessageSource |
| Resources, environment, profiles | Basic | ✅ ResourceLoader, Environment |
| Use | Inside frameworks | Always 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@Beanmethods in@Configurationclasses- auto-configuration (Spring Boot)
- programmatic registration (
BeanRegistrarin 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.
- Load bean definitions (scanning, configuration classes, auto-configuration imports).
- Run
BeanFactoryPostProcessors. They modify definitions before any bean exists. Example:ConfigurationClassPostProcessorprocesses@Configuration, and property placeholders are resolved. - Register
BeanPostProcessors. - Instantiate non-lazy singletons in dependency order. Inject dependencies, call
Awarecallbacks and init methods;BeanPostProcessors may wrap each bean, for example in AOP proxies for@Transactional. - Publish
ContextRefreshedEvent. Spring Boot then starts the web server, runsApplicationRunners andCommandLineRunners, and publishesApplicationReadyEvent.
| Extension point | Works on | Typical use |
|---|---|---|
BeanFactoryPostProcessor / BeanDefinitionRegistryPostProcessor | Definitions | Register or modify definitions, process config classes |
BeanPostProcessor | Bean instances | Wrap in proxies (AOP), process annotations (@Autowired, @PostConstruct) |
SmartInitializingSingleton | After all singletons exist | Post-startup wiring |
ApplicationListener | Events | React 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
...
}
}
| Situation | Resolution |
|---|---|
| Exactly one bean of the type | Injected |
| Several beans | @Primary, @Qualifier, or the parameter name matching the bean name; otherwise NoUniqueBeanDefinitionException |
| None | NoSuchBeanDefinitionException, 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 types | Repository<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
@Lazyon 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
@Primaryand@Qualifier; useObjectProviderfor optional or lazy dependencies. - Avoid cycles; prefer constructor injection; AOT moves work to build time.
References#
- Spring Framework reference, The IoC Container.
- Spring Framework reference, Container extension points.
- Spring Boot reference, Ahead-of-Time processing.