Spring IoC Container: BeanFactory vs ApplicationContext
Fast recall
Quick revision
Core answer
BeanFactory initializes beans lazily on first getBean() call; ApplicationContext eagerly creates all singletons at startup, which catches configuration errors immediately. ApplicationContext also auto-detects BeanPostProcessor beans, which is what makes @Autowired, @Transactional, and every annotation-driven feature work. In modern Spring, you never instantiate BeanFactory yourself — SpringApplication.run() creates an AnnotationConfigApplicationContext. BeanFactory exists for the framework-extension API.Understanding of the Spring container hierarchy and why ApplicationContext is the default choice.
Thinking they are interchangeable drop-in replacements, or not knowing that BeanFactory initializes beans lazily by default while ApplicationContext initializes singletons eagerly on startup.
Mention that ApplicationContext implements several sub-interfaces (MessageSource, ApplicationEventPublisher, ResourceLoader) which gives it the extra capabilities, and name concrete implementations like ClassPathXmlApplicationContext or AnnotationConfigApplicationContext.
Interview-ready guide
Interview answer
BeanFactory is Spring's root IoC interface — the contract that every container must implement. ApplicationContext extends it and adds everything a production application needs. In practice you never use BeanFactory directly.
The key difference: initialization strategy
BeanFactoryinitializes beans lazily — created only when first requested viagetBean()ApplicationContextcreates all singleton beans eagerly at startup — configuration errors, missing dependencies, and broken@PostConstructmethods fail immediately instead of on the first user request- In Kubernetes: eager initialization means a broken pod fails its readiness probe and never receives traffic; lazy initialization means the pod starts successfully and serves errors on the first request
What `ApplicationContext` adds over `BeanFactory`
- Auto-detection and registration of
BeanPostProcessorbeans — without this,@Autowired,@Transactional,@Async, and every annotation-driven feature stops working ApplicationEventPublisher— the infrastructure for Spring's event system and Spring Boot's startup eventsMessageSource— internationalization and message bundle resolutionResourceLoader— unified classpath, file system, and URL resource loading
When you touch `BeanFactory` directly
- Writing
BeanFactoryPostProcessorimplementations that modify bean definitions before beans are created - Writing
BeanPostProcessorimplementations that useBeanFactoryAwareto look up other beans during post-processing
Application code should always depend on ApplicationContext. BeanFactory is the framework-extension API.
Spring IoC Container: BeanFactory vs ApplicationContext
Direct Answer
BeanFactory is Spring's root IoC interface providing basic dependency injection, while ApplicationContext extends it with enterprise features: event publishing, internationalization, AOP auto-proxying, and eager singleton initialization. The practical difference is initialization strategy — BeanFactory initializes beans lazily on first getBean() call; ApplicationContext eagerly creates all singletons at startup, which catches configuration errors immediately. ApplicationContext also auto-detects BeanPostProcessor beans, which is what makes @Autowired, @Transactional, and every annotation-driven feature work. In modern Spring, you never instantiate BeanFactory yourself — SpringApplication.run() creates an AnnotationConfigApplicationContext. BeanFactory exists for the framework-extension API.
Code Example
Running both container types side-by-side makes the differences concrete. BeanFactory does lazy, on-demand instantiation; ApplicationContext eagerly pre-instantiates singletons and publishes lifecycle events.
public class ContainerComparison {
public static void main(String[] args) {
System.out.println("--- BeanFactory (lazy) ---");
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("beans.xml"));
// Singletons NOT yet instantiated — constructors haven't run.
UserService svc1 = bf.getBean(UserService.class); // constructor runs HERE
UserService svc2 = bf.getBean(UserService.class); // same instance cached
System.out.println("--- ApplicationContext (eager + events) ---");
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
// All singletons already constructed, @PostConstruct already executed,
// ContextRefreshedEvent already published BEFORE this line.
UserService svc3 = ctx.getBean(UserService.class);
}
}
@Configuration
@ComponentScan("com.example")
class AppConfig {}
@Component
class UserService {
@PostConstruct
void init() { System.out.println("UserService initialized"); }
@EventListener
public void onReady(ContextRefreshedEvent e) { /* only ApplicationContext fires this */ }
}
- With
BeanFactory, the constructor ofUserServicedoes not run untilgetBean()is called. - With
ApplicationContext, all singleton constructors,@PostConstructcallbacks, andBeanPostProcessorhooks run duringnew AnnotationConfigApplicationContext(...)— fail-fast on misconfiguration. @EventListeneronly works withApplicationContext, which is why Spring Boot always uses it.- Use
BeanFactoryonly for memory-constrained tools where startup time beats safety; useApplicationContextfor every real application.
Common Mistakes & Pitfalls
Thinking they are interchangeable drop-in replacements, or not knowing that BeanFactory initializes beans lazily by default while ApplicationContext initializes singletons eagerly on startup.
Check your understanding
Answer these without looking back at the article.
- What concrete implementations of ApplicationContext would you use for a Spring Boot app versus a standalone unit test?
- Why does eager singleton initialization in ApplicationContext help catch bugs earlier?
- What is a BeanFactoryPostProcessor and how does it differ from a BeanPostProcessor?
- How does ApplicationEventPublisher work and what events does Spring Boot publish during startup?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.