The ApplicationContext is Spring's central interface to the IoC container. It reads your configuration, creates and wires the beans, manages their lifecycle, and adds enterprise services on top of the basic BeanFactory: event publishing, message source internationalization, resource loading and access to the Environment with profiles and properties. In a Spring Boot application, SpringApplication.run() creates the context for you and returns it.
You rarely call the context directly in application code. Normally you declare beans and dependencies, and the context does the rest.
BeanFactory is the minimal container contract: it creates beans and resolves dependencies. ApplicationContext extends it, so everything a BeanFactory does is available, plus several features.
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Bean creation and dependency injection | Yes | Yes |
| Singleton creation | Lazy, on first request | Eager at startup by default |
| Event publishing | No | Yes, through ApplicationEventPublisher |
| Internationalized messages | No | Yes, through MessageSource |
| Resource loading | Limited | Yes, through ResourcePatternResolver |
Environment, profiles, properties | No | Yes |
Automatic BeanPostProcessor registration | Manual | Automatic |
Because singletons are created eagerly, a misconfigured bean fails at startup instead of at the first request. That fail-fast behaviour is one of the main reasons to use a context.
The context builds a bean definition for each bean it finds through component scanning, @Configuration classes with @Bean methods, or XML. It then instantiates the beans, injects dependencies, applies BeanPostProcessor callbacks such as proxy creation for @Transactional, and calls initialization hooks. On shutdown, it calls destroy callbacks in reverse dependency order.
The try-with-resources block closes the context, which runs the shutdown callbacks. In Boot the framework registers a shutdown hook for you.
The container injects dependencies as it creates each bean. Constructor injection is the recommended style: the fields can be final, the bean is never half-initialized, and tests can build it without Spring. When a class has a single constructor, @Autowired is optional.
Setter injection suits optional dependencies. Field injection with @Autowired works but hides dependencies and prevents final fields, so avoid it in new code.
Because the context is also an ApplicationEventPublisher, beans can publish and listen for events without knowing each other.
The same context supports MessageSource for translated messages, for example messageSource.getMessage("greeting", null, Locale.ENGLISH), and resource access such as context.getResource("classpath:data.txt"). The built-in context events, ContextRefreshedEvent and ContextClosedEvent, let you run code when startup finishes or shutdown begins.
You can look a bean up by type with context.getBean(MyService.class), by name, or by both. Treat this as a bootstrap or test tool, because calling getBean() inside business code turns the container into a service locator and hides dependencies.
The default scope is singleton: one shared instance per context. Use @Scope("prototype") for a new instance on every request. Web applications add request, session and application scopes. Do not inject a prototype or request-scoped bean into a singleton without a scoped proxy, or the singleton keeps the first instance forever.
| Implementation | Typical use |
|---|---|
AnnotationConfigApplicationContext | Standalone applications and tests with Java configuration |
ClassPathXmlApplicationContext | Legacy XML configuration on the classpath |
AnnotationConfigServletWebServerApplicationContext | Spring Boot servlet web applications |
AnnotationConfigReactiveWebServerApplicationContext | Spring Boot WebFlux applications |
GenericApplicationContext | Programmatic registration of beans |
In Spring Boot you do not choose one by hand. Boot picks the type from the classpath and the application type.
Use ApplicationContext. BeanFactory is the lower-level contract and is mainly relevant to framework internals or very constrained environments.
Usually yes, but contexts can form a parent-child hierarchy. For example, a root context for services and child contexts for web layers, as in Spring Cloud bootstrapping or older Spring MVC setups.
Inject it as a constructor parameter, or implement ApplicationContextAware. Prefer injecting the specific collaborator you need.
Singleton beans are created eagerly during context refresh, unless marked @Lazy. Prototype beans are created each time they are requested.
The ApplicationContext is the engine of a Spring application. It creates and wires beans, manages their scopes and lifecycle, publishes events, resolves messages and resources, and exposes the Environment. Prefer constructor injection, avoid getBean() in business code, and let Spring Boot create and close the context for you.