<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(140) 評論(0)  編輯  收藏 所屬分類: Scala / Java

    主站蜘蛛池模板: 四虎影在线永久免费观看| 一个人免费高清在线观看| 亚洲精品无码久久毛片 | 亚洲午夜久久久久妓女影院| 亚洲AV第一成肉网| 国产网站免费观看| 久久久久亚洲精品无码网址色欲| 免费观看的av毛片的网站| 亚洲精品无码久久久久YW| 在线视频免费国产成人| 牛牛在线精品免费视频观看| 亚洲精品无码久久毛片| 大地资源中文在线观看免费版| 亚洲成av人片天堂网| 亚洲黄色免费网站| 国产 亚洲 中文在线 字幕| 狠狠久久永久免费观看| 免费看黄福利app导航看一下黄色录像| 亚洲 自拍 另类小说综合图区| 一级毛片免费播放视频 | 亚洲综合区图片小说区| 久久久高清免费视频 | 国产亚洲美女精品久久| 亚洲日韩国产成网在线观看| 97无码人妻福利免费公开在线视频 | 亚在线观看免费视频入口| 亚洲天堂一区二区三区四区| 性色av免费观看| 91av免费在线视频| 亚洲成a人片77777老司机| 福利免费观看午夜体检区| 色视频在线观看免费| 亚洲Aⅴ无码专区在线观看q| 美女被免费喷白浆视频| 成人在线免费视频| 亚洲综合精品香蕉久久网97| 日韩视频在线免费观看| 久久免费国产视频| 亚洲精品无码成人片久久不卡| 国内精品久久久久久久亚洲| 国产精品视频免费观看|