Dependency Injection prior to Spring 2.5

Dependency Injection prior to Spring 2.5


Posted in : Spring Posted on : November 23, 2011 at 6:47 PM Comments : [ 0 ]

Under this heading, you will find the dependency injection prior to Spring version 2.5.

Dependency Injection prior to Spring 2.5

Under this heading, you will find the dependency injection prior to Spring version 2.5.

Dependency Injection or Inversion of control

The Inversion of control or Dependency Injection is the Spring feature which give us the concept that you don't need to create object but need to describe how they should be created. Also, you need to configure (in a XML ) which service is use by which component and don't need to link each component with related services. The IOC(Inversion of control) container do all this for you.

In simple words. it is used to define the object dependencies on each other.

Types of dependency Injection

There are two types of dependency Injections :

  • Setter Injection

  • Constructor Injection

Setter Injection

In the setter injection, dependencies are assigned via setter methods(JavaBeans properties).

The code for the helper class using setter method is given below :

package com.devmanuals.output;

import com.devmanuals.output.IOProducer;

public class HelperClass
{
IoProducer outputProducer;

public void setOutputProducer(IoProducer outputProducer){
this.outputProducer = outputProducer;
}
}

The XML file for configuring bean which set the dependency through property tag (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="HelperClass" class="com.devmanuals.output.HelperClass">
<property name="outputProducer">
<ref bean="XmlOutputProducer" />
</property>
</bean>

<bean id="XmlOutputProducer" class="com.devmanuals.output.impl.XmlOutputProducer" />
<bean id="CsvOutputProducer" class="com.devmanuals.output.impl.CsvOutputProducer" />

</beans>

In the above code, a XmlOutputProducer  bean injects into object of HelperClass  through  setOutputProducer( ) setter method.

 

Constructor Injection

In the Constructor Injection, the dependencies are assigned via a constructor.

The code for the helper class using constructor is given below :

package com.devmanuals.output;

import com.devmanuals.output.IOProducer;

public class HelperClass
{
IoProducer outputProducer;

HelperClass(IoProducer outputProducer){
this.outputProducer = outputProducer;
}
}

The XML file for configuring bean which set the dependency through constructor-arg tag (constructor 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="HelperClass" class="com.devmanuals.output.HelperClass">
<constructor-arg>
<bean class="com.devmanuals.output.impl.XmlOutputProducer" />
</constructor-arg>
</bean>

<bean id="XmlOutputProducer" class="com.devmanuals.output.impl.XmlOutputProducer" />
<bean id="CsvOutputProducer" class="com.devmanuals.output.impl.CsvOutputProducer" />

</beans>

In the above code, a XmlOutputProducer  bean injects into object of HelperClass  through constuctor.

 

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics