Tutorial Series
- Spring Framework Introduction
- Spring Framework module
- Spring development environment to build (Eclipse)
- Create a simple Spring application
- Spring Inversion of Control container (Inversion of Control - IOC)
- Appreciated dependency injection (DI - Dependency Injection)
- Bean XML configuration (1) - through XML configuration loaded Bean
- Bean XML configuration (2) - Bean scope and lifecycle callback method configuration
- Bean XML configuration (3) - configuration dependency injection
- Bean XML configuration (4) - Automatic Assembly
- Bean annotation (Annotation) Configuration (1) - Load Bean via annotations
- Bean annotation (Annotation) Configuration (2) - Bean scope and lifecycle callback method configuration
- Bean Annotations (Annotation) configuration (3) - configuration dependency injection
- Bean Java Configuration
- Spring Aspect Oriented Programming (AOP)
- Spring event (1) - built-in event
- Spring event (2) - Custom Event
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 Sword
replace the gun Gun
. Should Sword
be 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, Weapon
instances 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 Weapon
subclass.
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 Player
classes, 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.