Chan Chen Coding...

Understand Dependency Injection

What is Dependence Injection?
Dependency Injection (DI) is a design pattern in object-oriented programming. The core principle is to separating behavior from dependency resolution. This is a technology to decoupling the highly dependent software component. In other words, the purpose of DI is to allow program to select multi implementation among a given dependency interface at run time, or via configure file, rather than at compile time. Jakob Jenkov said that ‘Dependency Injection is a style of object configuration in which and object field and collaborators are set by external entity.’
Code Example:
public class MyDAO{
    private DataSource dataSource = new DataSourceImp(driver, url, username, password);
    
    // other data access methon
    public Customer readCustomer(int primaryKey){
        
    }
        
}
 
At data access object layer, in order to connect to database, MyDAO class need to have a DataSource instance. In this code example, to obtain the database connection, MyDAO class have to create an DataSource instance, and implement DataSourceImp with four hard code string. Therefore, MyDAO has a ‘dependency’ on MyDAO and some implementations of it. The MyDAO itself instantiaties as its Datasource implementations. Therefore the MyDAO class is said to ‘satisfy its own dependencies’. The disadvantage of ‘satisfy its own dependency’ is inflexible, if the database configuration is changed, we have to dive into the code, and change the four hard code string.
Construct Injection
To improve the MyDAO class, we can do a bit change on coding.
public class MyDAO{
    private DataSource dataSource;
    public MyDAO(DataSource dataSource){
        this.dataSource = dataSource;
    }

    // other data access methon
    public Customer readCustomer(int primaryKey){
        
    }
    
}
 
In this example, we inject the DataSource into constructor instead of the four hard code string parameters. Now the MyDAO class no longer depends on DataSource, we can inject any DataSource into the constructor. However, this does not mean we just get grid of those four hard code string parameter, we just pass these parameters to our client side.
Injection Container
The purpose of injection container is to tell the system where and how to inject the dependency of all components in the system. The reasons why call it container is that it takes more responsibility than just instantiating objects and injecting dependencies.
When configuring a dependency injection container you define what components it should be able to instantiate, and what dependencies to inject into each component. In addition you can normally configure the instantiation mode for each component. For instance, should a new instance be created every time? Or should the same component instance be reused (singleton) everywhere it is injected?
There are several dependency injection containers available for Java. In this article, we are going to take a look on Spring.
Inject in Spring
An example of using Spring
Person Interface:
public interface Person{
    public void useAxe();
}
 
Axe Interface:
public interface Axe{
    public void chop();
}
Dependency injection (DI) in object-oriented computer programming is a design pattern with a core principle of separating behavior from dependency resolution. In other words: a technique for
Implement Person:
public class Chinese implements Person{
    private Axe;
    private Person(){}
   
    //setter method for Spring set value injection
    public void setAxe(Axe axe){
        this.Axe = axe;
    }
    
    public void useAxe(){
        axe.chop();
    }
}
 
Implement Axe
public class StoneAxe implement Axe{
    public void chop(){
        System.out.println(“Stone Axe”);
    }
}
Spring Configuration XML
<?xml version="1.0" encoding="gb2312"?>
<BEANS>
    <BEAN style=lee.Chinese id=chinese>
    <property name="axe">
        <REF local="”stoneAxe”/">
    </property>
    </BEAN>
    <!-- define stoneAxe bean -->
    <BEAN style=lee.StoneAxe id=stoneAxe />
</BEANS>
Append
http://tutorials.jenkov.com/dependency-injection/index.html
http://developer.51cto.com/art/200610/33311.htm 



-----------------------------------------------------
Silence, the way to avoid many problems;
Smile, the way to solve many problems;

posted on 2012-02-18 15:49 Chan Chen 阅读(134) 评论(0)  编辑  收藏 所属分类: Scala / Java


只有注册用户登录后才能发表评论。


网站导航: