What is the difference between BeanFactory and ApplicationContext?

7 minintermediatebeanfactoryapplicationcontextspring-core

Quick Answer

BeanFactory is the root interface providing the basic IoC container functionality — bean definitions, dependency injection — with lazy initialization of singleton beans by default. ApplicationContext is a superset that adds enterprise features: eager singleton initialization by default, event publication, internationalization (MessageSource), environment/property abstraction, and easier integration with AOP — it's what virtually every Spring application actually uses.

Detailed Answer

BeanFactory is the most basic Spring IoC container interface — it can look up beans, resolve their dependencies, and manage bean scopes, but that's essentially it. Its most distinguishing default behavior: it lazily instantiates singleton beans only when they're first requested via getBean().

ApplicationContext extends BeanFactory and adds a substantial set of enterprise-oriented features on top:

  • Eager singleton initialization by default — singleton beans are created at container startup, not on first use, surfacing configuration errors immediately rather than at some arbitrary later point.
  • Event publicationApplicationEventPublisher/@EventListener support for in-process, decoupled communication between beans.
  • MessageSource — internationalization/localized message resolution.
  • Environment abstraction — unified access to properties from multiple sources (application.properties, environment variables, JVM system properties, profiles).
  • Easier AOP integration and automatic BeanPostProcessor/BeanFactoryPostProcessor registration.
ApplicationContext ctx = SpringApplication.run(MyApp.class, args);
MyService service = ctx.getBean(MyService.class);

In practice: virtually every real Spring (and Spring Boot) application interacts with an ApplicationContext (specifically, Spring Boot uses an auto-configured AnnotationConfigServletWebServerApplicationContext or similar) — BeanFactory is mostly relevant as the conceptual base interface and for very memory-constrained scenarios where its lighter weight and lazy-by-default behavior matter.