🚀 SpringBootApplicationContext Annotation – Complete Notes


1️⃣ Introduction

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.


2️⃣ What is ApplicationContext?

📌 Definition

ApplicationContext is a central interface in Spring that:

It is an advanced version of:

BeanFactory

🏗️ Core Responsibilities

FeatureDescription
Bean ManagementCreates and manages beans
Dependency InjectionResolves @Autowired dependencies
Lifecycle HandlingCalls @PostConstruct, @PreDestroy
Event SystemPublishes and listens to events
InternationalizationMessageSource support
Resource LoadingLoads files from classpath, URL

3️⃣ How @SpringBootApplication Creates ApplicationContext

🔹 Main Class Example

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

🔍 What Happens Internally?

When you call:

SpringApplication.run(MyApplication.class, args);

Spring Boot:

  1. Detects the type of application (Servlet / Reactive)
  2. Creates appropriate ApplicationContext:
  3. Performs component scanning
  4. Registers beans
  5. Starts embedded server (Tomcat/Jetty/Undertow)
  6. Publishes startup events

4️⃣ Internal Flow Diagram

main()
   ↓
SpringApplication.run()
   ↓
Create ApplicationContext
   ↓
Load Environment
   ↓
Scan Components (@ComponentScan)
   ↓
Register Beans
   ↓
Refresh Context
   ↓
Start Embedded Server
   ↓
Application Ready

5️⃣ Structure of @SpringBootApplication

@SpringBootApplication is a combination of:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

📘 Breakdown

1️⃣ @SpringBootConfiguration

2️⃣ @EnableAutoConfiguration

Example:

3️⃣ @ComponentScan


6️⃣ Types of ApplicationContext in Spring Boot

Application TypeContext Used
Servlet-based (Spring MVC)AnnotationConfigServletWebServerApplicationContext
Reactive (WebFlux)AnnotationConfigReactiveWebServerApplicationContext
Non-web appAnnotationConfigApplicationContext

7️⃣ Accessing ApplicationContext Manually

🔹 Method 1: Inject It

@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);
        }
    }
}

🔹 Method 2: Get Bean Manually

ApplicationContext context =
        new AnnotationConfigApplicationContext(MyConfig.class);

MyService service = context.getBean(MyService.class);

⚠️ Manual creation is rarely needed in Spring Boot applications.


8️⃣ Bean Lifecycle Inside ApplicationContext

1. Bean Instantiation
2. Dependency Injection
3. @PostConstruct
4. Bean Ready
5. @PreDestroy (on shutdown)

Example

@Component
public class DemoBean {

    @PostConstruct
    public void init() {
        System.out.println("Bean initialized");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Bean destroyed");
    }
}

9️⃣ Real-World Example

UserController
     ↓
UserService
     ↓
UserRepository

Spring Boot:


🔟 ApplicationContext vs BeanFactory

FeatureBeanFactoryApplicationContext
Lazy InitializationYesNo (default eager)
Events Support
AOP IntegrationBasicFull
Internationalization
Recommended

1️⃣1️⃣ Important Notes


1️⃣2️⃣ Common Interview Questions

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.


🎯 Summary


If you'd like, I can also provide:

Just say Download and mention the format.