<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

    主站蜘蛛池模板: 一区二区免费视频| caoporm超免费公开视频| 免费A级毛片在线播放| 亚洲不卡中文字幕无码| 久久嫩草影院免费看夜色| 亚洲精品国产V片在线观看| 日韩免费码中文在线观看| 亚洲国产精品一区二区九九| 成人精品国产亚洲欧洲| 亚洲国产精品成人久久蜜臀| 免费夜色污私人影院网站电影| 亚洲伦理一二三四| 国产卡二卡三卡四卡免费网址 | 无码日韩人妻AV一区免费l | 国产成A人亚洲精V品无码| 一区二区在线视频免费观看| 亚洲色偷偷综合亚洲AV伊人| 亚洲高清免费视频| 亚洲国产精品久久久久| 在线视频精品免费| 国产精品亚洲专区一区| 最新精品亚洲成a人在线观看| youjizz亚洲| 可以免费观看一级毛片黄a| 免费一区二区无码视频在线播放| 99久久免费看国产精品| 亚洲一卡2卡4卡5卡6卡残暴在线| 中国黄色免费网站| 亚洲av日韩av不卡在线观看| 亚洲最大免费视频网| 亚洲久热无码av中文字幕| 中文字幕日韩亚洲| 91精品国产免费网站| 国产亚洲精品美女久久久久| 精品亚洲永久免费精品| 日韩毛片免费无码无毒视频观看 | 国产亚洲精品美女久久久| 成人免费视频69| 国产亚洲情侣久久精品| 亚洲国产精品嫩草影院在线观看| 亚洲丁香婷婷综合久久|