0% found this document useful (0 votes)
6 views7 pages

Understanding Spring IoC Container

The document explains the Inversion of Control (IoC) Container in the Spring Framework, detailing its definition, functions, and types, including BeanFactory and ApplicationContext. It outlines how the IoC container works, including bean creation, dependency injection, and lifecycle management, along with configuration methods such as XML and annotations. Additionally, it compares ClassPathXmlApplicationContext and FileSystemXmlApplicationContext regarding their usage, portability, and error handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Understanding Spring IoC Container

The document explains the Inversion of Control (IoC) Container in the Spring Framework, detailing its definition, functions, and types, including BeanFactory and ApplicationContext. It outlines how the IoC container works, including bean creation, dependency injection, and lifecycle management, along with configuration methods such as XML and annotations. Additionally, it compares ClassPathXmlApplicationContext and FileSystemXmlApplicationContext regarding their usage, portability, and error handling.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

:SPRING BOOT:

🔹 Definition of IoC Container:


IOC Container (Inversion of Control Container) is a core component of Spring
Framework that creates, manages, and injects dependencies (objects) automatically.
It inverts the control of object creation from programmer → Spring container.

🔹 What IOC Container Does


1. Create objects (beans)
2. Manage bean lifecycle
3. Inject dependencies (Constructor / Setter / Field)
4. Manage configurations
 XML
 Java @Configuration
 Component Scanning (@Component, @Service, @Repository)
5. Provide singleton or prototype bean instances
6. Connect beans together based on configuration

🔹 Types of IOC Containers in Spring


1 BeanFactory ([Link])
1️⃣
 Basic IOC container
 Lazy initialization (bean created only on request)
 Good for memory-constrained environments
 Less features

2️⃣ ApplicationContext
([Link])
 Advanced container
 Eager initialization (creates beans at startup)
 Supports:
 AOP
 Internationalization (i18n)
 Event handling
 Annotation-based configuration & Most commonly used in Spring apps
🔹 How IOC Container Works (Flow)
1. Spring reads configuration
➤ [Link] / @Configuration class / Component scan
2. Container creates all required beans
3. Container injects dependencies automatically
4. Beans become ready for use
5. Container manages bean lifecycle (init → use → destroy)

XML:
<bean id="student" class="[Link]" />

Java Code:

ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");

Student s = [Link]("student", [Link]);

====================================================================
===

Configuration:
1. XML
2. Annotation-based

=> After implements of java classes, spring framework provides an xml formats to
configure java classes.

=> XML file: format, Rules & Regulations


=> Inside you spring application/ project , create an xml file
=> [Link]

<?xml version="1.0" encoding="UTF-8"?>


<beans
xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]">

<!-- bean definitions here -->


</beans>

NOTE: make sure your system connected to internet


====
Bean:
====
<bean id="address" class="[Link]">

</bean>

-> parse XML file to spring / IOC container.


-> Spring framework api, API is set of classes and interface
-> Creating an object for adress defined class of an implementation of
interface is called a container.

Spring framework:
By using below to interfaces and its asscociated implementation classes, we
are going to create spring IOC container.

1. BeanFactory
2. ApplicationContext

-> create an object for ApplicationContext, so that container created


-> in java, for an interface object creation is not possible
-> An Interface can hold/refer it’s implemented class Object.

Interface object creation :


Interface A { }

class B implements A{

A a = new B();

-------------------------------------------------------------------------------------------------------
***Difference Between below two classes:
1. [Link]
&
2. [Link] class

Feature ClassPathXmlApplicationContext FileSystemXmlApplicationContext


Loads Spring configuration file from
Loads Spring configuration file from
Definition file system (absolute or relative
classpath
path)
Inside the project →
Location of Any location on disk (outside project
src/main/resources or classpath
XML also allowed)
folders
Usage When config is packaged inside When config is kept externally and
Feature ClassPathXmlApplicationContext FileSystemXmlApplicationContext
can be changed without rebuilding
JAR/WAR
project
Path "C:/configs/[Link]" or
"[Link]"
Example "config/[Link]"
More portable (works across Less portable (depends on exact file
Portability
different systems) path)
Preferred Web apps, enterprise apps using Desktop apps / external runtime
For in-project resources configs
ClassLoader Uses ClassLoader to load file Uses File I/O to read XML from disk
Error If File
Classpath not found File path not found
Missing

creation of container:

ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");

ApplicationContext context =
new
FileSystemXmlApplicationContext("C:/spring/config/[Link]")

=> ClassPathXmlApplicationContext loads XML from classpath (project).


=> FileSystemXmlApplicationContext loads XML from any file system path.

You might also like