Inversion Of Control(IOC) and Dependency Injection

Spring Framework It was developed by Rod Johnson in 2003. Spring framework makes the easy development of JavaEE application. Spring is a lightweight framework. It can be thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF, etc. JavaEE is used to create web application. Initially Struts framework is used, which has tightly coupled relationship. Spring is light weight and loosely coupled relationship. It makes easy to create JavaEE web applications. How Spring is lightweight means using Jar files without using WAR files Spring framework is used to create standalone and web applications. What is IOC? In Spring, Inversion of Control (IoC) is a design principle where the framework manages object creation and dependencies, rather than the application code, achieved through Dependency Injection (DI ). What is Dependency Injection: Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application. Dependency Injection makes our programming code loosely coupled. For Example consider We have Car and Engine classes. Car class is fully depends with Engine class so if we do any change in Engine class we have to do in Car class also. This is called tightly coupled relationship. To drive the car we need Engine class object. `Class Car{ drive(){ Engine engine=new Engine(); engine.start(); } } Class Engine{ start(){ System.out.println(“Engine Started”); } If we do any changes now like this Class Engine{ String fuel1,fuel2; Engine(String fuel1,String fuel2){ this. fuel1= fuel1; this. fuel2= fuel2; } start(){ System.out.println(“Engine Started”); }` Let's understand using following code how the classes are bring to loosely coupled relationship from tightly coupled relationship: Note: We should not create objects using new keyword to make classes are loosely coupled relationship. Engine class Car class Main method But we have to make these classes as lightly coupled relationship. That is achieved by using Inversion of Control(IOC). IOC makes the code loosely coupled. In such case, there is no need to modify the code if our logic is moved to new environment. In Spring framework, IOC container is responsible to inject the dependency. We provide metadata to the IOC container either by XML file or annotation. So, In Spring, objects are created in Spring container memory. This is also called as Context(Environment). This Context is ConfigurableApplicationContext. Spring keeps all the reference’s in the Context which we create for all classes. Now components(objects) are created in SpringContainer using @Component Annotation. Now we have to tell the spring to keep those references in Context(Environment). ConfigurableApplicationContext is provided by running the method SpringApplication.run() which is in main method. Now we got the container. In the container we have to keep car class component so us getBean() method to do this. So when the drive method is called from car class it calls engine class start method because already using @Autowired we connected both components. Annotations which are used in this program to make classes loosely coupled relationship: @Component @AutoWired @SpringBootApplication IoC Container(Spring Container) The IoC container is responsible to instantiate, configure and assemble the objects. The IoC container gets informations from the XML file and works accordingly. The main tasks performed by IoC container are: • to instantiate the application class • to configure the object • to assemble the dependencies between the objects Autowiring in Spring Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection. Autowiring can't be used to inject primitive and string values. It works with reference only. Beans In Spring, the objects managed by the IoC container are called beans.

Apr 9, 2025 - 22:37
 0
Inversion Of Control(IOC) and Dependency Injection

Spring Framework
It was developed by Rod Johnson in 2003. Spring framework makes the easy development of JavaEE application.
Spring is a lightweight framework. It can be thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF, etc.

JavaEE is used to create web application. Initially Struts framework is used, which has tightly coupled relationship. Spring is light weight and loosely coupled relationship. It makes easy to create JavaEE web applications.
How Spring is lightweight means using Jar files without using WAR files Spring framework is used to create standalone and web applications.

What is IOC?
In Spring, Inversion of Control (IoC) is a design principle where the framework manages object creation and dependencies, rather than the application code, achieved through Dependency Injection (DI ).

What is Dependency Injection:
Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application. Dependency Injection makes our programming code loosely coupled.

For Example consider We have Car and Engine classes. Car class is fully depends with Engine class so if we do any change in Engine class we have to do in Car class also. This is called tightly coupled relationship.
To drive the car we need Engine class object.
`Class Car{
drive(){
Engine engine=new Engine();
engine.start();
}
}
Class Engine{
start(){
System.out.println(“Engine Started”);
}

If we do any changes now like this
Class Engine{
String fuel1,fuel2;
Engine(String fuel1,String fuel2){
this. fuel1= fuel1;
this. fuel2= fuel2;
}
start(){
System.out.println(“Engine Started”);
}`

Let's understand using following code how the classes are bring to loosely coupled relationship from tightly coupled relationship:

Note: We should not create objects using new keyword to make classes are loosely coupled relationship.
Engine class
Image description
Car class

Image description
Main method

Image description

But we have to make these classes as lightly coupled relationship. That is achieved by using Inversion of Control(IOC).

IOC makes the code loosely coupled. In such case, there is no need to modify the code if our logic is moved to new environment.
In Spring framework, IOC container is responsible to inject the dependency. We provide metadata to the IOC container either by XML file or annotation.

So, In Spring, objects are created in Spring container memory. This is also called as Context(Environment). This Context is ConfigurableApplicationContext.
Spring keeps all the reference’s in the Context which we create for all classes.
Now components(objects) are created in SpringContainer using @Component Annotation. Now we have to tell the spring to keep those references in Context(Environment).
ConfigurableApplicationContext is provided by running the method SpringApplication.run() which is in main method.
Now we got the container. In the container we have to keep car class component so us getBean() method to do this. So when the drive method is called from car class it calls engine class start method because already using @Autowired we connected both components.

Annotations which are used in this program to make classes loosely coupled relationship:
@Component
@AutoWired
@SpringBootApplication

IoC Container(Spring Container)
The IoC container is responsible to instantiate, configure and assemble the objects. The IoC container gets informations from the XML file and works accordingly. The main tasks performed by IoC container are:
• to instantiate the application class
• to configure the object
• to assemble the dependencies between the objects

Autowiring in Spring
Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.
Autowiring can't be used to inject primitive and string values. It works with reference only.

Beans
In Spring, the objects managed by the IoC container are called beans.