What is the difference between BeanFactory and ApplicationContext?

7 minintermediatebeanfactoryapplicationcontextspring-core

Quick Answer

BeanFactory is the root interface providing 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 — 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 set of enterprise-oriented features on top:

  • Eager singleton initialization by default — singleton beans are created at container startup, not on first use. This surfaces configuration errors immediately instead of 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: almost every real Spring (and Spring Boot) application interacts with an ApplicationContext — specifically, Spring Boot uses an auto-configured AnnotationConfigServletWebServerApplicationContext or similar. BeanFactory mostly matters as the conceptual base interface, and in memory-constrained scenarios where its lighter weight and lazy-by-default behavior matter.