Spring Dependency Injection (DI)

In Spring frameowork, Dependency Injection (DI) design pattern is used to define the object dependencies between each other. It exits in two major types :

  • Setter Injection
  • Constructor Injection

1. Setter Injection

This is the most popular and simple DI method, it will injects the dependency via a setter method.

Example

A helper class with a setter method.

package com.mkyong.output;
 
import com.mkyong.output.IOutputGenerator;
 
public class OutputHelper
{
	IOutputGenerator outputGenerator;
 
	public void setOutputGenerator(IOutputGenerator outputGenerator){
		this.outputGenerator = outputGenerator;
	}
 
}

A bean configuration file to declare the beans and set the dependency via setter injection (property tag).

<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 injects a ‘CsvOutputGenerator’ bean into ‘OutputHelper’ object via a setter method (setOutputGenerator).

2. Constructor Injection

This DI method will injects the dependency via a 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 configuration file to declare the beans and set the dependency via constructor injection (constructor-arg tag).

<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 injects a ‘CsvOutputGenerator’ bean into ‘OutputHelper’ object via a constructor.

Setter or Constructor injection?

There are no hard rule set by Spring framework, just use whatever type of DI that suit your project needs. However, due to the simplicity of the setter injection, it’s always selected for most of the scenarios.

Reference

1. http://en.wikipedia.org/wiki/Dependency_injection

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • deepa

    good for short time

  • Ram

    Nice post

  • Naresh

    Its good you studied and putting here to help all of us.we appreciate the work….

  • Anjali

    Hi,

    Your tutorials are very useful. It helps me to understand the concepts in short time.
    I have a doubt regarding the DI. What about @Resource, @Autowire, @Qualifier, @Configurable annotations? Do they define new ways of Dependency Injection?

  • hai

    Apress – Spring Recipes A Problem Solution Approach – book is good for spring

  • ananthagowri

    im new for spring.i want more basic things about strings.

  • bob gal

    hi …is it possible to add two numbers in spring please tell me….

  • Chris

    How do I create a list of beans then inject the list ??
    i.e. something like this :-

    &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&quot;&gt;
     
    &lt;List id=&quot;Helpers&quot;  class=&quot;com.mkyong.output.Helper&quot; &gt;
    	&lt;bean id=&quot;OutputHelper&quot; &gt;
    		&lt;constructor-arg&gt;
    			&lt;bean class=&quot;com.mkyong.output.impl.CsvOutputGenerator&quot; /&gt;
    		&lt;/constructor-arg&gt;
    	&lt;/bean&gt;
      	&lt;bean id=&quot;inputHelper&quot; &gt;
    		&lt;constructor-arg&gt;
    			&lt;bean class=&quot;com.mkyong.output.impl.CsvIputGenerator&quot; /&gt;
    		&lt;/constructor-arg&gt;
    	&lt;/bean&gt;
    &lt;/List&gt;
     
    &lt;/beans&gt;

    So that in my code i can just instantiate a list of helpers.
    (apologies if theres an easy way of doing this, Im a Spring newbie)

    • Dharmi

      you can definetely inject dependency for lists. something like this.

      &lt;bean id=&quot;helpers&quot; class=&quot;com.mkyong.output.Helper&quot;&gt;
      	&lt;property name=&quot;listofHelperObjects&quot;&gt;
      		&lt;set&gt;
      			&lt;ref bean=&quot;outputHelper&quot;/&gt;
      			&lt;ref bean=&quot;inputHelper&quot;/&gt;
      		&lt;/set&gt;
      	&lt;/property&gt;
      &lt;/bean&gt;
  • Pingback: Spring Tutorials()