In Spring Boot, ApplicationContext is the heart of the framework.
It is responsible for:
Although there is no direct annotation called @SpringBootApplicationContext, the ApplicationContext is automatically created and configured by:
@SpringBootApplication
So when people say “SpringBootApplicationContext annotation”, they usually mean:
> How @SpringBootApplication bootstraps and creates the ApplicationContext.
ApplicationContext is a central interface in Spring that:
It is an advanced version of:
BeanFactory
| Feature | Description |
|---|---|
| Bean Management | Creates and manages beans |
| Dependency Injection | Resolves @Autowired dependencies |
| Lifecycle Handling | Calls @PostConstruct, @PreDestroy |
| Event System | Publishes and listens to events |
| Internationalization | MessageSource support |
| Resource Loading | Loads files from classpath, URL |
@SpringBootApplication Creates ApplicationContext@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
When you call:
SpringApplication.run(MyApplication.class, args);
Spring Boot:
main()
↓
SpringApplication.run()
↓
Create ApplicationContext
↓
Load Environment
↓
Scan Components (@ComponentScan)
↓
Register Beans
↓
Refresh Context
↓
Start Embedded Server
↓
Application Ready
@SpringBootApplication is a combination of:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
Example:
| Application Type | Context Used |
|---|---|
| Servlet-based (Spring MVC) | AnnotationConfigServletWebServerApplicationContext |
| Reactive (WebFlux) | AnnotationConfigReactiveWebServerApplicationContext |
| Non-web app | AnnotationConfigApplicationContext |
@Component
public class MyBean {
private final ApplicationContext context;
public MyBean(ApplicationContext context) {
this.context = context;
}
public void printBeans() {
String[] beans = context.getBeanDefinitionNames();
for (String bean : beans) {
System.out.println(bean);
}
}
}
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyService service = context.getBean(MyService.class);
⚠️ Manual creation is rarely needed in Spring Boot applications.
1. Bean Instantiation
2. Dependency Injection
3. @PostConstruct
4. Bean Ready
5. @PreDestroy (on shutdown)
@Component
public class DemoBean {
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
@PreDestroy
public void destroy() {
System.out.println("Bean destroyed");
}
}
UserController
↓
UserService
↓
UserRepository
Spring Boot:
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Lazy Initialization | Yes | No (default eager) |
| Events Support | ❌ | ✅ |
| AOP Integration | Basic | Full |
| Internationalization | ❌ | ✅ |
| Recommended | ❌ | ✅ |
Q1: Is ApplicationContext a Bean?
No, it is the container that manages beans.
Q2: Can we have multiple ApplicationContexts?
Yes (Parent-Child context hierarchy).
Q3: When is ApplicationContext created?
During SpringApplication.run() execution.
Q4: What is context refresh?
Re-initializing the bean factory and reloading configuration.
If you'd like, I can also provide:
Just say Download and mention the format.