Language Translator

Showing posts with label flexibility. Show all posts
Showing posts with label flexibility. Show all posts

Spring Dependency Injection and IOC


DI is a technique to make the code loosely coupled and classes independent by injecting the dependencies externally rather than creating or finding the dependency within the class. So, that we can reuse the code/classes independently.

DI inturn also being called as inversion of control (IOC) also, because the responsibility of deciding and injecting the dependency is not dependent on the class rather it is injected by container at runtime.

In Spring dependencies to a class can be injected in two main ways:
a) Setter Injection : When the dependencies are injecting through setters, we called them as setter injection
b) Constructor injection: When the dependencies inject through the constructors we called them as constructor injection.

I would like and try to clear this by an example (using setter injection):

Suppose,

a) I have a class (DAO class) name "MyDAO" implementing interface DAO, having some database accesslogic in it. for example: some read, insert and delete functions.

interface DAO{

  public boolean insertObject(Object obj);
  public Object readObject(int objId);
  public boolean deleteObject(Object obj);

}

class MyDAO implements DAO {

  private DBConnection connection;

  public boolean insertObject(Object obj){
return connection.getConnection().performInsert(obj);

   }

  public Object readObject(int objId){
return connection.getConnection().readObj(objId);

   }

  public boolean deleteObject(Object obj){
return connection.getConnection().deleteObj(objId);
   }

  public DBConnection getDBConnection(){
return this.connection;
   }

  //setter injection
  public void setDBConnection(DBConnection con){    
this.connection = con;
   }

}

b) Similarly, have three classes specific to the type of database connection, implementing same interface name as "DBConnection".

interface DBConnection{
  public Connection getConnection();
}

class OracleConnection implements DBConnection{
  public Connection getConnection(){...}

}

class DB2Connection implements DBConnection{
 public Connection getConnection(){...}

}

class MySQLConnection implements DBConnection{
 public Connection getConnection(){...}
}


c) **If you have noticed, the reference of this same interface we have given above in the "MyDAO" class, this is the point where injection will occur.

The setter injection of any of the above database connection can be done to "MyDAO" class can be done by even standard java, with the help of helper classes. But, as we are using Spring, we will use dependency injection (declarative injection using xml file) provided by Spring container.

So, create a Spring bean configuration file name as "Spring-Config.xml" and declare all the dependencies in it.

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="myDao" class="MyDAO">
<property name="dbConnection" ref="oracleDBConnection" /> //Setter injection
</bean>

<bean id="oracleDBConnection" class="OracleConnection " />
<bean id="db2DBConnection" class="DB2Connection" />
<bean id="mysqlDBConnection" class="MySQLConnection" />

</beans>

d) If you observed by above, we can refer/inject any of the database connection (oracle, db2, mysql) to "MyDAO" class with very little change in spring configuration file "Spring-Config.xml".


e) We can call the MyDAO class in our application,lets take class having java main method

class Main{
public static void main (String arr[]){

int objId=1;

ApplicationContext context =
      new ClassPathXmlApplicationContext(new String[] {"Spring-Cinfig.xml"});

    DAO dao = (DAO)context.getBean("myDao");
Object obj = dao.readObject(objId);

}
}

If you have observed all the way down till now,
a) I have made MyDAO class loosely coupled and can use any type database connection with very very minor change in the XML file.
b) This will help in easy testing of MyDao class logic using connection mockobjects for unit testing.
c) This makes my project very flexible, scalable and maintainable. This point is vital in case of large enterprise java projects.