Difficulty: Intermediate
What is the difference between BeanFactory and ApplicationContext? Which one do you use in a Spring Boot application?
Both of these are the Spring container, the object that creates and wires your beans, but they sit at different levels of capability. Picture BeanFactory as the engine of a car and ApplicationContext as the full car with steering, dashboard, and air conditioning built around that engine. Everything BeanFactory does, ApplicationContext does too, because ApplicationContext extends the BeanFactory interface.
BeanFactory is the root interface of the container. Its core job is simple: given a bean name or type, return an instance, and manage dependency injection. Its typical implementation, DefaultListableBeanFactory, creates beans lazily, meaning a bean is only instantiated when somebody first asks for it through getBean. That was useful historically for memory-constrained environments such as applets, but for a modern server application the trade-off is poor because configuration mistakes appear late, at the first request that touches the bean.
ApplicationContext adds enterprise features. It publishes and listens to application events through ApplicationEventPublisher. It supports internationalisation through MessageSource. It resolves resources such as files and classpath entries. It reads Environment properties and profiles. It automatically registers BeanPostProcessors and BeanFactoryPostProcessors, which is how annotations like @Autowired, @Transactional, and @Async are actually honoured. And by default it eagerly instantiates all singleton beans at startup, so wiring errors surface immediately when the app boots, not in production traffic.
In Spring Boot, you always end up with an ApplicationContext. SpringApplication.run returns a ConfigurableApplicationContext. For servlet web apps the concrete type is AnnotationConfigServletWebServerApplicationContext, and for reactive apps a reactive variant. You will rarely call getBean yourself, because injecting dependencies is the point, but knowing the context lets you explain what happens at startup: scan components, register bean definitions, run post-processors, instantiate singletons, start the embedded server, publish ContextRefreshedEvent and ApplicationReadyEvent.
Common interview probes include: when would you still use BeanFactory directly? Almost never in application code; it is used internally or in very lightweight frameworks. Is eager initialisation always good? Not always. If startup time matters, for example in serverless or CLI tools, you can enable spring.main.lazy-initialization=true or annotate specific beans with @Lazy, trading faster startup for later failures. Another probe is the difference between FactoryBean and BeanFactory. They sound alike but are unrelated: a FactoryBean is a bean that itself produces other objects, while BeanFactory is the container.
To close your answer, state the practical takeaway: use ApplicationContext, understand that it is a BeanFactory plus enterprise services, and remember that eager singleton creation is why Spring apps fail fast on misconfiguration.
@SpringBootApplication
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);
// Rarely needed, but useful to explore
System.out.println(ctx.getBeanDefinitionCount() + " beans registered");
OrderService svc = ctx.getBean(OrderService.class);
System.out.println(ctx.getEnvironment().getProperty("server.port", "8080"));
}
}
@Component
class StartupListener {
@EventListener(ApplicationReadyEvent.class)
public void onReady() {
System.out.println("Application is ready to serve traffic");
}
}
The context gives you bean lookup, environment access, and event publishing, all beyond what a plain BeanFactory offers.
ApplicationContext, BeanFactory, Eager Initialisation, Container