?
Solved

Inversion of Control and Dependency injection

Posted on 2009-04-27
11
Medium Priority
?
464 Views
Last Modified: 2013-11-23
Hi Experts,
                   I am going to try my hands on Spring MVC, along with Hibernate.

                   I am very confused over the Inversion of Control and dependency injection concepts. Can someone help me understanding this from beginners point of view.
0
Comment
Question by:aman0711
  • 5
  • 4
  • 2
11 Comments
 
LVL 12

Expert Comment

by:Gibu George
ID: 24244046
The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.

i.e., Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects.
0
 
LVL 12

Accepted Solution

by:
Gibu George earned 1000 total points
ID: 24244054
Benefits of IOC (Dependency Injection) are as follows:
   1)  Minimizes the amount of code in your application. With IOC containers you do not care about how services are created and how you get references to the ones you need. You can also easily add additional services by adding a new constructor or a setter method with little or no extra configuration.
   2)  Make your application more testable by not requiring any singletons or JNDI lookup mechanisms in your unit test cases. IOC containers make unit testing and switching implementations very easy by manually allowing you to inject your own objects into the object under test.
   3)  Loose coupling is promoted with minimal effort and least intrusive mechanism. The factory design pattern is more intrusive because components or services need to be requested explicitly whereas in IOC the dependency is injected into requesting piece of code. Also some containers promote the design to interfaces not to implementations design concept by encouraging managed objects to implement a well-defined service interface of your own.
   4)  IOC containers support eager instantiation and lazy loading of services. Containers also provide support for instantiation of managed objects, cyclical dependencies, life cycles management, and dependency resolution between managed objects etc.
0
 
LVL 10

Author Comment

by:aman0711
ID: 24244077
Hi Gibu..
 
               Could you please tell me a little more on this line?

             >> The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created.

              I dont really get, "You do not create your objects but describe how they should be created"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:brunoguimaraes
brunoguimaraes earned 1000 total points
ID: 24244784
Suppose you have a bean class with a service attribute. You don't instantiate the service, but you tell which service needs to be instantiated in a configuration file.

See attached code, this is kind of an example.
// Java file
public class Bean {
 
   private Service service;
 
   public someMethod() {
       // notice I didn't instantiate the service class any where
       service.executeTask();
   }
 
}
 
// XML file (here there is information on how to instantiate the service, but the container decides when to instantiate it)
<bean id="service" class="com.ee.Service" />
<bean id="bean" class="com.ee.Bean">
    <property name="service" ref="service" />
</bean>

Open in new window

0
 
LVL 10

Author Comment

by:aman0711
ID: 24244948
Hi brunoguimaraes,

                      Your examply definitely cleared my mind bit more....  but why do we want to do this? what harm one can encounter if we instantiate the Service right there
0
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 24245051
See gibu_george's second post, he explains all the benefits (less code, loose coupling, etc).
0
 
LVL 10

Author Comment

by:aman0711
ID: 24245131
ok got it.
Please correct me if I am wrong...

Lets say I made a class abc.

Now there is another class xyz, thats need a reference to the object of class abc.
so instead of doing abc newObject = new abc();  in xyz class....

we define this in XML file amd Spring framework, automatically creates that object when required by xyz class?

I am sorry, Asking too much stuff...
0
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 24245193
Yes, that is correct. But you don't wanna do this for every attribute in your class. Typically you have an attribute that is an interface, and in the config file you say which implementation class you want to use.

For example:

// an interface that defines methods for connecting to a database, and other DB related stuff
public interface IDao {
}
 
// an implementation of that interface, specific to Oracle databases
public class OracleDao implements IDao {
}
 
// an implementation of that interface, specific to MySql databases
public class MySqlDao implements IDao {
}
 
// business class that need the dao's methods
public class BusinessObject() {
  
   private IDao dao;   
}
 
// XML file
<bean id="oracleDao" class="com.ee.OracleDao" />
<bean id="mySqlDao" class="com.ee.MySqlDao" />
<bean id="businessObject" class="com.ee.BusinessObject">
     <!-- here you choose which implementation to use. it is currently using Oracle, but if you want to change databases later, you just replace ref="oracleDao" for ref="mySqlDao", and there is no need to recompile the code -->
    <property name="dao" ref="oracleDao" />
</bean>

Open in new window

0
 
LVL 10

Author Comment

by:aman0711
ID: 24245233
Got it,
            This was a very good example for a beginner like me :-)

           So this is what whole dependency injection is.

            Thank you...
0
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 24245267
You're welcome!
0
 
LVL 10

Author Closing Comment

by:aman0711
ID: 31575080
Thanks folks:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question