Spring(2)-Spring Dependency Injection(DI)


package com.mkyong.output;
 
import com.mkyong.output.IOutputGenerator;
 
public class OutputHelper
{
	IOutputGenerator outputGenerator;
 
	public void setOutputGenerator(IOutputGenerator outputGenerator){
		this.outputGenerator = outputGenerator;
	}
 
}
In Spring frame eowork、Dependency Injection(DI)design pattern is used to define the object dependencies between each other.It exits in two mar jotypes:
  • Setter Injection
  • Constructor Injection
  • 1.Setter Injection
    This the most popur and simple DI method、it will inject the dependency via setter method.
    Example
    A helper class with a setter method.
    A bean configration file to declare the beans and set the dependency via setter injection.
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
    	<bean id="OutputHelper" class="com.mkyong.output.OutputHelper">
    		<property name="outputGenerator">
    			<ref bean="CsvOutputGenerator" />
    		</property>
    	</bean>
     
    <bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" />
    <bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" />
     
    </beans>
    
    You just inject a‘CsvOutput Generator’bean into‘Output Helper’object via setter method.
    2.コンストラクタInjection
    This DI method will inject the dependency via constructor.
    Example
    A helper class with a constructor.
    package com.mkyong.output;
     
    import com.mkyong.output.IOutputGenerator;
     
    public class OutputHelper
    {
    	IOutputGenerator outputGenerator;
     
            OutputHelper(IOutputGenerator outputGenerator){
    		this.outputGenerator = outputGenerator;
    	}
    }
    
    A bean configration file to declare the beans and set the dependency via contructor injection.
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
    	<bean id="OutputHelper" class="com.mkyong.output.OutputHelper">
    		<constructor-arg>
    			<bean class="com.mkyong.output.impl.CsvOutputGenerator" />
    		</constructor-arg>
    	</bean>
     
    <bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" />
    <bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" />
     
    </beans>
    
    You just inject a‘CsvOutput Generator’bean into‘Output Helper’object via construct.
    Setter or Costructor injection?
    The re are no hard rule set by Spring frame ework、juse whatever type of DI that suit your project needs.However、due to the simplity of the setter injection、it’s always selected for most of the scenars.
    Reference
    1.http://en.wikipedia.org/wiki/Dependency_injection