Appreciated dependency injection (DI - Dependency Injection)


Tutorial Series


Dependency injection (Dependency Injection, DI) is a design pattern, is one of the core concept of the Spring Framework. Its role is to remove dependencies between Java class that implements the loose coupling, in order to develop a test. To better understand DI, DI first understand the problem to be solved.

Tight coupling problem

If you use a class, a natural approach is to create an instance of the class:

class Player{  
    Weapon weapon;  
    
    Player(){  
        // 与 Sword类紧密耦合
        this.weapon = new Sword();  
        
    }  
    
    public void attack() {
        weapon.attack();
    }
}   

Tight coupling problem of this method, for example, the player's only weapon is a sword Sword, but not be able to Swordreplace the gun Gun. Should Swordbe changed Gun, all the code involved to be modified, of course, in a small code size when it is simply not a problem, but the code is large-scale, time-consuming effort of will.

Dependency Injection

Dependency injection is a dependency relationship between a design pattern based eliminated. For example, A class dependent class B, A Class not directly create Classes B, but to this dependency on external xml configuration file (or java config file), and then created by the Spring container according to configuration information, management bean class .

Example:

class Player{  
    Weapon weapon;  
    
    // weapon 被注入进来
    Player(Weapon weapon){  
        this.weapon = weapon;  
        
    }  
    
    public void attack() {
        weapon.attack();
    }
    
    public void setWeapon(Weapon weapon){  
        this.weapon = weapon;  
    }  
}   

As indicated above, Weaponinstances of classes in the code is not created, but rather pass through the outer constructor is passed type parentWeapon , it may be passed in any object type Weaponsubclass.

Incoming which subclass, the xml file may be arranged outside (or java config file), Spring create the desired container configuration example of a sub-class, and injected into Playerclasses, as follows:

    <bean id="player" class="com.qikegu.demo.Player"> 
        <construct-arg ref="weapon"/>
    </bean>
    
    <bean id="weapon" class="com.qikegu.demo.Gun"> 
    </bean>

The above code <construct-arg ref="weapon"/>ref points to id="weapon"the bean, the type of weapon is passed Gun, if you want to change Sword, can be modified as follows:

    <bean id="weapon" class="com.qikegu.demo.Sword"> 
    </bean>

Just at this configuration can be modified.

Loosely coupled, do not coupled. A Class B Class dependent, there is a close coupling between class A and class B, if the parent class B0 dependency becomes dependent Class A Class B, in dependence of the class A and class B0, B0 class A class may be used in any of its subclasses, a dependencies between classes and subclasses of class B0 are loosely coupled.

DI technology can be seen that the basis of polymorphism and reflection.

Guess you like

Origin www.cnblogs.com/jinbuqi/p/10959104.html