What is an ApplicationContext, and what are the main concrete implementations you would instantiate?
answer
- BeanFactory + enterprise features
- AnnotationConfig / ClassPathXml / Generic
- convenience ctors call refresh() for you
- Generic = you call refresh()
- close() runs destroy callbacks
basics
~10 sApplicationContext is Spring's IoC container that creates, wires, and manages beans. Common implementations: AnnotationConfigApplicationContext (Java/annotation config), ClassPathXmlApplicationContext (XML config), and GenericApplicationContext (flexible, programmatic registration).
solid answer
~30 sApplicationContext is Spring's central IoC container: it reads bean definitions, instantiates beans, injects dependencies, and adds enterprise features on top of a plain BeanFactory (event publishing, message source/i18n, resource loading, environment/property access). The three you construct directly are AnnotationConfigApplicationContext (scans/registers @Configuration and @Component classes), ClassPathXmlApplicationContext (loads bean definitions from XML on the classpath), and GenericApplicationContext (a general-purpose context where you register bean definitions or call registerBean yourself, then invoke refresh()). In practice most apps use AnnotationConfigApplicationContext; Spring Boot uses a subclass (AnnotationConfigServletWebServerApplicationContext / AnnotationConfigApplicationContext) under the hood.
code
java · 13 lines// Annotation/Java config — refresh() runs inside this constructor
try (var ctx = new AnnotationConfigApplicationContext(AppConfig.class)) {
MyService svc = ctx.getBean(MyService.class);
svc.run();
}
// XML config
var xmlCtx = new ClassPathXmlApplicationContext("beans.xml");
// Generic — YOU register then refresh exactly once
var generic = new GenericApplicationContext();
new AnnotatedBeanDefinitionReader(generic).register(AppConfig.class);
generic.refresh();go deeper
Name the three implementations and state ApplicationContext is the IoC container that builds and injects beans.
Explain it extends BeanFactory with events/i18n/resources/environment, and note which constructors auto-call refresh().
Contrast the class hierarchies (Generic vs Refreshable) and eager singleton instantiation, and mention Boot's web context subclass.
Discuss when you'd pick GenericApplicationContext for programmatic bootstrap, lifecycle/close semantics, and how the context participates in the broader startup pipeline.
**IoC container.** Inversion of Control means the framework, not your code, is responsible for constructing objects and supplying (injecting) their dependencies. In Spring the container that does this is described by two interfaces: `BeanFactory` (the minimal container: get a bean by name/type, lazy instantiation) and `ApplicationContext`, which *extends* `BeanFactory` and layers on enterprise features: - **Event publishing** — `ApplicationEventPublisher` / `ApplicationListener`. - **Message source** — i18n via `MessageSource`. - **Resource loading** — `getResource(...)` returning `Resource` (classpath:, file:, URL). - **Environment abstraction** — `Environment`, property sources, active profiles. - **Automatic detection and application of `BeanFactoryPostProcessor` / `BeanPostProcessor`** (their *mechanics* are covered by a sibling topic; here just note the context wires them for you). **Concrete implementations you construct directly:** 1. `AnnotationConfigApplicationContext` — configured from Java: pass `@Configuration` classes and/or base packages to `scan(...)`. Detects `@Component`, `@Service`, `@Bean`, etc. It *extends* `GenericApplicationContext`. 2. `ClassPathXmlApplicationContext` — loads `<bean>` definitions from one or more XML files on the classpath. Sibling `FileSystemXmlApplicationContext` loads from the filesystem. These extend `AbstractRefreshableApplicationContext`. 3. `GenericApplicationContext` — a flexible base you use when you want to register bean definitions programmatically (via readers like `XmlBeanDefinitionReader`, `AnnotatedBeanDefinitionReader`, `ClassPathBeanDefinitionScanner`, or the functional `registerBean`) and then call `refresh()` yourself exactly once. **Key behavioral difference — who calls refresh():** The annotation and XML convenience contexts call `refresh()` for you inside their constructor (the constructor that takes config classes / xml locations). A bare `GenericApplicationContext` does *not*: you register beans and then call `refresh()` manually. **When to use which:** modern apps use annotation/Java config (`AnnotationConfigApplicationContext`, or Boot's auto-created web variant). XML contexts appear in legacy codebases. `GenericApplicationContext` is used for programmatic/functional bean registration, tests, and embedding Spring in a custom bootstrap. **Gotchas:** Always `close()` the context (or use try-with-resources — `ConfigurableApplicationContext` implements `AutoCloseable`) so `@PreDestroy`/destroy callbacks and `SmartLifecycle.stop()` run. Registering `registerShutdownHook()` ties context close to JVM shutdown.
- Which of these implementations calls refresh() automatically and which does not?The convenience constructors of AnnotationConfigApplicationContext and ClassPathXmlApplicationContext (the ones taking config classes / xml locations) call refresh() themselves. A bare GenericApplicationContext (and AnnotationConfigApplicationContext's no-arg constructor) require you to call refresh() manually after registering beans.
- How does ApplicationContext differ from BeanFactory?ApplicationContext extends BeanFactory and adds event publishing, i18n MessageSource, resource loading, the Environment/profile abstraction, and eager singleton pre-instantiation plus automatic post-processor detection. BeanFactory is the lower-level, lazy container rarely used directly.
saying these in an interview costs you the question
- Saying ApplicationContext and BeanFactory are unrelated / competing containers
- Claiming ClassPathXmlApplicationContext can only hold XML-declared beans and can't use annotations
- Thinking every context lazily creates singletons (contexts eagerly pre-instantiate non-lazy singletons on refresh)
- Forgetting to close the context so destroy callbacks never fire