Dependency Injection Concept
Nov 30th, 2009 | By admin | Category: SpringJava classes and objects should be as much as possible loosely coupled with other java classes and objects. The loosely-coupled behavior allows a Java code to be re-usable ,test-worthy , modular and well maintained. Often , A class depends on another class and arise the coupling .
Say a class FetchUserDataFromDB depends on a class called DataBaseAccess, so it is obvious that , one would try to make a coupling by creating an instance of DataBaseAccess class in FetchUserDataFromDB class and call it’s methods. Here , we introduce a tight coupling and a strong dependency.
Look at the following scenarios :
Scenario 1 : Tight Coupling
Tight Coupling
Step 1 : Get me a database connection object from the pool.
Step 2 : Take the database connection object conn.
Step 3 : Thanks you , get me the User’s data whose id is 007.
Step 4 : Here it is , His name is James Bond .
Step 5 : Enough , Don’t want the conn anymore. Close it.
Step 6 : Ok , closed and conn is taken back to pool.
Scenario 2 : Loose Coupling
Loose Coupling
Step 1 : Oh , you need a connection ? Injecting a connection conn from the Connection Pool.
Step 2 : Fine , you need the user’s Information whose id is 007 ? Injecting the information ; He is James Bond.
Step 3 : Look like you are not using the conn anymore. Injecting a service to close the connection and get the conn back to the pool.
Now the million dollar question is , which of these scenarios you would like to see in your code/project. Undoubtedly the scenario 2 where you do not have the burden of keeping a reference to the databaseaccess object and your client class is no more tightly coupled .
Scenario 2 is the depiction of the Dependency Injection. Where the container injects the dependencies to the client classes and takes care of a full life cycle.
There are several advantages of dependency injection.
- Your code is modular.
- Classes are not having any strong reference to another class.
- You are able to test your code independently.
The container which supports this dependency injection concept is called the IOC (Inversion of control) Container. Spring uses the concept of IOC and dependency injection to locate and inject services for you. Spring allows you to write your classes without having any dependency code .
There are three different ways dependencies can be injected.
- Constructor Injection (CI) - Dependencies (say , other class references) can be injected as a parameter to the constructor of a class. Hence no hard code object creation of the Dependent class is needed.
- Setter Injection (SI) - Inject the dependency as a setter method of the class
- Interface Injection (II) – interface injection, in which the exported module provides an interface that its users must implement in order to get the dependencies at runtime.
A design based on independent classes / components increases the re-usability and possibility to test the software. For example if a class A expects a Dao (Data Access object) for receiving the data from a database you can easily create another test object which mocks the database connection and inject this object into A to test A without having an actual database connection.
Have look at the Bean dependency in detail Here .
