Week 1 Day 2 — ApplicationContext and BeanFactory
Goal
Today I want to understand the Spring container more deeply.
Main questions:
- What is
ApplicationContext? - What is
BeanFactory? - What is the difference between
ApplicationContextandBeanFactory? - What is a
BeanDefinition? - What happens when Spring creates beans?
- Why is
ApplicationContextimportant for the exam?
1. Big Picture
In Day 1, I learned that Spring creates and manages objects.
Those managed objects are called beans.
But now the question is:
Where does Spring store and manage those beans?
The answer is:
Inside the Spring container.
The most important Spring container is:
ApplicationContext
2. What Is ApplicationContext?
ApplicationContext is the main Spring container.
It is responsible for:
- creating beans
- wiring dependencies
- managing bean lifecycle
- reading configuration
- resolving properties
- supporting profiles
- publishing events
- supporting internationalization
- integrating with AOP
- providing access to beans
Simple definition:
ApplicationContextis the main Spring container that creates, configures, wires, and manages Spring beans.
Exam definition:
ApplicationContextis a central interface in Spring that represents the IoC container and provides bean management plus enterprise features such as events, resources, messages, and environment access.
3. Simple Mental Model
Imagine ApplicationContext as a smart object registry.
It knows all Spring beans.
Example:
ApplicationContext
------------------------------------------------
bean name bean object
------------------------------------------------
orderService OrderService
orderRepository OrderRepository
emailService EmailService
paymentController PaymentController
When Spring creates OrderService, it sees that OrderService needs OrderRepository.
So it injects the correct bean.
OrderService needs OrderRepository
ApplicationContext has OrderRepository
ApplicationContext injects OrderRepository into OrderService
4. Example Without Spring
Without Spring, I create objects manually:
OrderRepository orderRepository = new OrderRepository();
OrderService orderService = new OrderService(orderRepository);
This means my code is responsible for:
- object creation
- dependency wiring
- configuration
- lifecycle
5. Example With Spring
With Spring, I write:
@Repository
public class OrderRepository {
}
@Service
public class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
Spring creates the objects and connects them.
The ApplicationContext manages them.
6. How to Access a Bean Manually
Usually, I do not need to get beans manually.
But for learning, it is useful.
Example:
ApplicationContext context =
SpringApplication.run(MyApplication.class, args);
OrderService orderService = context.getBean(OrderService.class);
This asks the Spring container:
Please give me the OrderService bean.
Spring returns the managed bean.
7. Important Exam Trap
This object is managed by Spring:
OrderService orderService = context.getBean(OrderService.class);
This object is not managed by Spring:
OrderService orderService = new OrderService(new OrderRepository());
Why?
Because the first object comes from the Spring container.
The second object is created manually with new.
Memory sentence:
Objects from
ApplicationContextare Spring-managed beans. Objects created manually withneware normal Java objects.
8. What Is BeanFactory?
BeanFactory is the basic Spring container interface.
It provides the basic ability to:
- create beans
- configure beans
- wire dependencies
- retrieve beans
Simple definition:
BeanFactoryis the basic IoC container in Spring.
Example idea:
BeanFactory factory = ...;
OrderService orderService = factory.getBean(OrderService.class);
In modern Spring applications, I usually work with ApplicationContext, not directly with BeanFactory.
9. ApplicationContext vs BeanFactory
ApplicationContext extends BeanFactory.
That means:
ApplicationContext = BeanFactory + more features
Comparison:
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Basic bean creation | yes | yes |
| Dependency injection | yes | yes |
| Bean lookup | yes | yes |
| Event publishing | no/basic | yes |
| Internationalization | no/basic | yes |
| Resource loading | limited | yes |
| Environment and profiles | limited | yes |
| Application events | no/basic | yes |
| Commonly used in Spring Boot | no | yes |
Simple memory sentence:
BeanFactoryis the basic container.ApplicationContextis the full-featured container used in most Spring applications.
10. Lazy vs Eager Bean Creation
This is important for the exam.
BeanFactory
Traditionally, BeanFactory creates beans lazily.
That means:
Create bean only when requested.
ApplicationContext
By default, ApplicationContext creates singleton beans eagerly at startup.
That means:
Create singleton beans when the application starts.
This helps Spring find configuration errors early.
Example:
If OrderService needs OrderRepository, but no OrderRepository bean exists, Spring can fail during startup.
This is good because the application does not start in a broken state.
11. Important: Singleton Beans Are Created at Startup
In Spring, most beans are singleton by default.
So when the application starts, Spring usually creates them immediately.
Example:
@Service
public class OrderService {
}
By default, Spring creates one OrderService bean during startup.
Unless lazy initialization is enabled.
12. What Is a BeanDefinition?
Before Spring creates a bean, it has metadata about the bean.
This metadata is called a BeanDefinition.
A BeanDefinition describes how to create a bean.
It can include:
- bean class
- bean name
- scope
- constructor arguments
- dependencies
- lifecycle methods
- lazy or eager behavior
- init method
- destroy method
Simple definition:
A
BeanDefinitionis metadata that tells Spring how to create and manage a bean.
13. BeanDefinition Example
When Spring sees this:
@Service
public class OrderService {
}
Spring registers a bean definition internally.
Conceptually:
BeanDefinition
------------------------------------------------
bean name: orderService
bean class: OrderService
scope: singleton
lazy: false
dependencies: OrderRepository
Then Spring uses this metadata to create the actual bean object.
14. BeanDefinition vs Bean
This difference is important.
| Concept | Meaning |
|---|---|
| BeanDefinition | Recipe / metadata for creating a bean |
| Bean | Actual object created and managed by Spring |
Memory sentence:
BeanDefinitionis the recipe. Bean is the real object.
Example:
Recipe: how to make pizza
Pizza: actual food
BeanDefinition: how to create OrderService
Bean: actual OrderService object
15. What Happens When Spring Boot Starts?
Simplified flow:
1. main() method runs
2. SpringApplication.run(...) starts
3. ApplicationContext is created
4. Spring reads configuration
5. Component scanning finds classes
6. BeanDefinitions are registered
7. Singleton beans are created
8. Dependencies are injected
9. Lifecycle callbacks run
10. Application is ready
Important:
Spring does not immediately create beans blindly.
First it reads configuration and registers bean definitions.
Then it creates actual bean objects.
16. Startup Flow with Example
Imagine I have:
@Repository
public class OrderRepository {
}
@Service
public class OrderService {
private final OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
Spring startup:
1. Finds OrderRepository
2. Registers BeanDefinition for orderRepository
3. Finds OrderService
4. Registers BeanDefinition for orderService
5. Creates OrderRepository bean
6. Creates OrderService bean
7. Sees OrderService constructor needs OrderRepository
8. Injects OrderRepository bean
9. OrderService is ready
17. Can I Have Multiple ApplicationContexts?
Yes.
A Spring application can technically have multiple application contexts.
But in normal Spring Boot applications, I usually think about one main ApplicationContext.
In web applications, there can be parent-child contexts in some setups, but this is more advanced.
For the certification, the most important point is:
ApplicationContextis the main container used by Spring applications.
18. Common ApplicationContext Implementations
Spring has different implementations.
Examples:
AnnotationConfigApplicationContext
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
WebApplicationContext
Modern Spring Boot usually creates the correct context automatically.
For annotation-based configuration, a common one is:
AnnotationConfigApplicationContext
Example:
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
But in Spring Boot, I normally use:
SpringApplication.run(MyApplication.class, args);
19. Manual Mini Example
This is useful for theory.
@Configuration
public class AppConfig {
@Bean
public OrderRepository orderRepository() {
return new OrderRepository();
}
@Bean
public OrderService orderService(OrderRepository orderRepository) {
return new OrderService(orderRepository);
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
OrderService service = context.getBean(OrderService.class);
}
}
Here, Spring creates the beans from Java configuration.
No Spring Boot is required.
This proves that Spring Framework and Spring Boot are not the same thing.
Spring Boot makes startup easier, but the container comes from Spring Framework.
20. Important Exam Traps
Trap 1
ApplicationContext and BeanFactory are not the same level.
Correct:
ApplicationContext extends BeanFactory and adds more features.
Trap 2
A Spring bean is not created only by @Component.
Beans can also be created by:
@Bean
Trap 3
A Java object created with new is not automatically Spring-managed.
OrderService service = new OrderService(...);
This is just a normal Java object.
Trap 4
BeanDefinition is not the same as a bean.
BeanDefinition = metadata
Bean = actual object
Trap 5
ApplicationContext usually creates singleton beans eagerly at startup.
That is why many dependency errors appear when the app starts.
21. Interview Answer
Question:
What is
ApplicationContextin Spring?
Good answer:
ApplicationContext is the main Spring IoC container. It creates, wires, configures, and manages Spring beans. It reads configuration, registers bean definitions, creates singleton beans, injects dependencies, and manages lifecycle callbacks. It also provides extra features compared with BeanFactory, such as event publishing, resource loading, environment access, profiles, and internationalization. In most modern Spring and Spring Boot applications, ApplicationContext is the container we use.
22. Short Exam Answer
Question:
What is the difference between
BeanFactoryandApplicationContext?
Answer:
BeanFactory is the basic IoC container that provides bean creation and dependency injection. ApplicationContext extends BeanFactory and adds more enterprise features such as events, internationalization, resource loading, environment access, and easier integration with Spring features. In modern Spring applications, ApplicationContext is used most often.
Practice Questions and Answers
Question 1
What is ApplicationContext?
Answer:
ApplicationContext is the main Spring container. It creates, wires, configures, and manages Spring beans. It also provides features such as event publishing, environment access, profiles, resource loading, and lifecycle management.
Question 2
What is BeanFactory?
Answer:
BeanFactory is the basic Spring IoC container. It can create beans, wire dependencies, and return beans when requested. It is more basic than ApplicationContext.
Question 3
What is the difference between BeanFactory and ApplicationContext?
Answer:
BeanFactory is the basic container for bean creation and dependency injection. ApplicationContext extends BeanFactory and adds more features, such as event publishing, internationalization, resource loading, environment access, and profile support. Most modern Spring applications use ApplicationContext.
Question 4
What is a BeanDefinition?
Answer:
A BeanDefinition is metadata that tells Spring how to create and manage a bean. It can include the bean class, name, scope, dependencies, lifecycle methods, and lazy/eager behavior.
Question 5
What is the difference between a BeanDefinition and a bean?
Answer:
A BeanDefinition is the recipe or metadata for creating a bean. A bean is the actual object created and managed by Spring.
Memory sentence:
BeanDefinition is the recipe. Bean is the real object.
Question 6
Why can Spring detect missing dependencies during application startup?
Answer:
Spring can detect missing dependencies during startup because ApplicationContext usually creates singleton beans eagerly. When Spring creates a bean, it checks its constructor dependencies. If a required dependency does not exist, the application fails to start.
Question 7
Is this object Spring-managed?
OrderService service = context.getBean(OrderService.class);
Answer:
Yes. This object comes from the Spring ApplicationContext, so it is a Spring-managed bean.
Question 8
Is this object Spring-managed?
OrderService service = new OrderService(new OrderRepository());
Answer:
No. This object is created manually with new, so Spring does not automatically manage it.
Final Memory Sentences
ApplicationContextis the main Spring container.BeanFactoryis the basic IoC container.ApplicationContextextendsBeanFactory.ApplicationContextadds events, resources, profiles, environment access, and internationalization.BeanDefinitionis metadata for creating a bean.BeanDefinitionis the recipe. Bean is the object.- Objects from
ApplicationContextare Spring-managed. - Objects created manually with
neware not automatically Spring-managed. - Singleton beans are usually created eagerly at startup.