0% found this document useful (0 votes)
9 views2 pages

Spring_IoC_Container_Types

Inversion of Control (IoC) is a design principle in Spring that delegates object creation and management to the Spring IoC container, promoting loose coupling through dependency injection. There are two main types of IoC containers: BeanFactory, which is lightweight and suitable for memory-critical applications, and ApplicationContext, which offers advanced features like event handling and internationalization. The choice between them depends on the application's requirements, with BeanFactory being ideal for lightweight use and ApplicationContext for enterprise-level applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Spring_IoC_Container_Types

Inversion of Control (IoC) is a design principle in Spring that delegates object creation and management to the Spring IoC container, promoting loose coupling through dependency injection. There are two main types of IoC containers: BeanFactory, which is lightweight and suitable for memory-critical applications, and ApplicationContext, which offers advanced features like event handling and internationalization. The choice between them depends on the application's requirements, with BeanFactory being ideal for lightweight use and ApplicationContext for enterprise-level applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Spring IoC Container and Its Types

### What is IoC (Inversion of Control)?


Inversion of Control (IoC) is a design principle in Spring that shifts the responsibility of creating and managing objects
from the programmer to the Spring IoC container.
IoC helps in achieving loose coupling by injecting dependencies instead of creating them manually.

### Types of IoC Containers in Spring


Spring provides two major types of IoC containers:
1. **BeanFactory** (Basic IoC container)
2. **ApplicationContext** (Advanced IoC container)

#### 1. BeanFactory (org.springframework.beans.factory.BeanFactory)


- BeanFactory is the simplest container in Spring.
- It loads bean definitions and provides dependency injection when requested.
- It is lightweight and suitable for applications where memory and performance are critical.

Example:
```java
Resource resource = new ClassPathResource("spring-config.xml");
BeanFactory factory = new XmlBeanFactory(resource);
MyClass obj = (MyClass) factory.getBean("myBean");
```

#### 2. ApplicationContext (org.springframework.context.ApplicationContext)


- It is an advanced version of BeanFactory with additional enterprise-level features.
- Supports event propagation, declarative mechanisms, and internationalization.
- Pre-instantiates beans, unlike BeanFactory, which loads them lazily.

Example:
```java
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
MyClass obj = (MyClass) context.getBean("myBean");
```

### Comparison of BeanFactory and ApplicationContext

| Feature | BeanFactory | ApplicationContext |


|--------------------|--------------------------------|----------------------------------|
| Lazy Loading | Yes (Loads beans lazily) | No (Pre-instantiates beans) |
| Event Handling | No | Yes |
| Internationalization | No | Yes |
| Enterprise Features | No | Yes (AOP, Annotation Scanning) |
| Recommended Use | Lightweight applications | Enterprise applications |

### Final Thoughts


- Use **BeanFactory** when memory efficiency is a priority.
- Use **ApplicationContext** for full-fledged Spring applications with advanced features.

You might also like