Dec
28
2014

IOC(Inversion of control ) and DI(dependency Injection) in spring

Hi in this post of spring tutorial i will discuss about two very basic concept (IOC “Inversion of control”) and DI”dependency Injection”) of spring framework .These two term are often mixed by people, but actually (IOC “Inversion of control”) and DI”dependency Injection”) are different and both have different purpose .

 IOC (Inversion of control)


Inversion of control simply mean managing life cycle of object is relinquish over framework rather than application. In Spring Ioc container manages life cycle of bean of a java application . We are giving control to create object for application to spring container instead of creating object using new operator .

In other word giving control to container to get instance of object is called IOC (Inversion of control). It mean instead of you are creating object using new operator let the container do that for you.

Benefits of IOC 

  1. Code to create objects is centralized in configuration file.

Di(Dependency Injection)


Di(Dependency Injection) process of providing the dependencies to an object . A dependency is any other object the current object needs.

Now we will see the same how and when it has been used in our programming .

Target


We will understand IOC and DI with simple example of storing some registration data into table. Below is directory structure of  program.

IOC and DI

index.jsp

This will simply redirect to SignUp page via controller . signUp.html is looked in request mapping of controller class.

When @RequestMapping annotation with parameter  “signUp.html” found then method below that get execute .

signup.jsp

It is simple form . When clicking on submit button then @RequestMapping annotation with parameter specified in action is searched and method of related annotation get executed.

Person.java

This is Entity class which form table with property as column of the table. @Entity annotation used to declare a class as entity class. 

@Id annotation is used to define primary key for the table. @GeneratedValue annotation is used to generate primary key for table. 

PersonController.java

Controller handle request from client and return response back to client. When client request then controller that match @RequestMapping is searched and related method get executed .After processing response is return back.

Here PersonService class object is created by Spring container rather than creating it using new operator.This is called IOC(Inversion of control).

In spring-servlet.xml we have mention name of bean class PersonService . So spring container create object for this bean.

PersonService .java

 PersonService.java accept data from controller and submit DAO class .

Notice that  PersonService.java class has PersonDao variable “personDao” as property. This property is set up in spring-servlet.xml. Instead of setting up property using getter and setter method in java files after creating object we setup property using xml . This way of setting property is known as DI(Dependency injection).

Below code shows how PersonDao type variable is setup using DI.

PersonDao.java

 DAO(data access object) interact with databases to manipulate data.Here in above code we have save person class object .Here SessionFactory object is created by spring container .Below code is used when creating object of SessionFactory is created.

 

Web.xml

It send request with .html pattern to spring-servlet.xml .

Spring-servlet.xml

Spring container read this file to create object and to set property of classes . This creation of object is called IOC and providing dependency is called DI.

 

This is all about DI and IOC . I will get back soon with next spring tutorial post.

Comments are closed.