<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无码成人网站在线观看| 最近中文字幕免费mv在线视频| 亚洲日本中文字幕区| 免费毛片a线观看| 久久精品国产亚洲77777| 亚洲视频在线免费观看| 亚洲国产精品综合福利专区| 国产精品成人观看视频免费 | 成人永久福利免费观看| 中国在线观看免费高清完整版 | 在线观看91精品国产不卡免费| 亚洲精品国产精品| 免费人成在线观看播放国产 | 亚洲国产人成网站在线电影动漫| 99re免费视频| 国产亚洲美女精品久久久| 在线免费视频你懂的| 亚洲AV无码专区电影在线观看| 成人免费视频一区二区| 国产偷v国产偷v亚洲高清| 8x8×在线永久免费视频| 亚洲人成无码网站在线观看| mm1313亚洲精品国产| 日韩精品人妻系列无码专区免费 | 国产精品亚洲一区二区三区在线观看| 亚洲午夜AV无码专区在线播放| 一区二区三区无码视频免费福利| 精品无码一区二区三区亚洲桃色 | 最新国产成人亚洲精品影院| 国产禁女女网站免费看| 在线涩涩免费观看国产精品| youjizz亚洲| 国产亚洲?V无码?V男人的天堂| 最近免费mv在线电影| 久久综合亚洲色hezyo| 亚洲国产人成网站在线电影动漫| 日本高清免费不卡在线| 无码免费一区二区三区免费播放| 亚洲色大成网站www尤物|