Inversion of Control and Dependency injection

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.
LVL 10
aman0711Asked:
Who is Participating?
 
Gibu GeorgeChief Technology OfficerCommented:
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
 
Gibu GeorgeChief Technology OfficerCommented:
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
 
aman0711Author Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
brunoguimaraesCommented:
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
 
aman0711Author Commented:
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
 
brunoguimaraesCommented:
See gibu_george's second post, he explains all the benefits (less code, loose coupling, etc).
0
 
aman0711Author Commented:
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
 
brunoguimaraesCommented:
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
 
aman0711Author Commented:
Got it,
            This was a very good example for a beginner like me :-)

           So this is what whole dependency injection is.

            Thank you...
0
 
brunoguimaraesCommented:
You're welcome!
0
 
aman0711Author Commented:
Thanks folks:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.