Spring: BeanFactory vs ApplicationContext

ApplicationContext is a BeanFactory plus events, i18n, resource loading and eager singleton creation.

Code
// BeanFactory: lazy - a bean is created on the first getBean()
// ApplicationContext: eager - singletons are created at startup

var ctx = new AnnotationConfigApplicationContext(AppConfig.class);
// every singleton already exists here
OrderService s = ctx.getBean(OrderService.class);
Output
Eager creation is a feature: a misconfigured bean fails at startup
rather than on the first request in production.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11