Dependency Injection is a design pattern which promotes loose coupling and helps client change dependency without modifying the code that depends on it.

Code Example Without Dependency Injection:

With below code snippet, if there is any change to Implementation, we have to modify below logic. For instance, if NumberGeneraorServiceImpl_NoDI is coded as mentioned below to generate random number and now one client wants to generate SequenceNumber in place of Random Number. This requirement forces us to modify the code.

public class NumberGeneratorServiceImpl_NoDI implements NumberGeneratorService {
	
	private INumberGenerator numberGenerator;
	
	public NumberGeneratorServiceImpl_NoDI(){
		numberGenerator=new RandomNumberGenerator();
	}
	@Override
	public int generateNumber() {		
		return numberGenerator.generate();
	}
	public INumberGenerator getNumberGenerator() {
		return numberGenerator;
	}
	public void setNumberGenerator(INumberGenerator numberGenerator) {
		this.numberGenerator = numberGenerator;
	}

}

RandomNumberGenerator.java

public class RandomNumberGenerator implements INumberGenerator {
	Random randomNumGenerator=new Random();
	@Override
	public int generate() {		
		return randomNumGenerator.nextInt(Integer.MAX_VALUE) ;
	}

}

To compile and test NumberGeneratorServiceImpl_NoDI.java, we have to have RandomNumberGenerator class. This complicates unit testing as well as reduces code reuse.

Above example with Spring Framework – Setter Injection

Remove the logic to instantiate objects with new operator.And inject RandonNumberGenerator implementation for INumberGenerator through setter injection

package com.codesimplify.springcore.examples.service.impl;

import com.codesimplify.springcore.examples.INumberGenerator;
import com.codesimplify.springcore.examples.service.NumberGeneratorService;

public class NumberGeneratorServiceImpl implements NumberGeneratorService {
	
	private INumberGenerator numberGenerator;
	@Override
	public int generateNumber() {		
		return numberGenerator.generate();
	}
	public INumberGenerator getNumberGenerator() {
		return numberGenerator;
	}
	public void setNumberGenerator(INumberGenerator numberGenerator) {
		this.numberGenerator = numberGenerator;
	}
}

If we see the code, we removed following line.

numberGenerator=new RandomNumberGenerator();

Add following configuration to spring configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">	

	<bean id="numberGeneratorService" name="numberGeneratorService"
		class="com.codesimplify.springcore.examples.service.impl.NumberGeneratorServiceImpl">
		<property name="numberGenerator" ref="randomNumberGenerator" />
	</bean>
	<bean id="randomNumberGenerator" name="randomNumberGenerator"
		class="com.codesimplify.springcore.examples.RandomNumberGenerator">
	</bean>
	 
</beans>

Dependency Injection via Constructor Example:

Let’s modify above code to show how to inject RandomNumberGenerator via constructor.

package com.codesimplify.springcore.examples.service.impl;

import com.codesimplify.springcore.examples.INumberGenerator;
import com.codesimplify.springcore.examples.service.NumberGeneratorService;

public class NumberGeneratorServiceImpl_CI implements NumberGeneratorService {
	
	private INumberGenerator numberGenerator;
	
	NumberGeneratorServiceImpl_CI(INumberGenerator numberGenerator){
		this.numberGenerator=numberGenerator;
	}
	
	@Override
	public int generateNumber() {		
		return numberGenerator.generate();
	}
	
}

Following Configuration will be changed.


<bean id="numberGeneratorService" name="numberGeneratorService"
class="com.codesimplify.springcore.examples.service.impl.NumberGeneratorServiceImpl_CI">
<constructor-arg ref="randomNumberGenerator" />
</bean>

If Client wants to replace RandomNumberGenerator with SequenceNumberGenerator, all he need to do is, implement INumberGenerator interface to generate sequence numbers and modify the configuration to replace randomNumberGenerator bean reference to sequenceNumberGenerator as shown below.

package com.codesimplify.springcore.examples;

public class SequenceNumberGenerator implements INumberGenerator{
	int currentSequenceVal=0;
	@Override
	public int generate() {
		return currentSequenceVal=currentSequenceVal+1;		
	}
	
}

 


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	

	<bean id="numberGeneratorService" name="numberGeneratorService"
		class="com.codesimplify.springcore.examples.service.impl.NumberGeneratorServiceImpl">
		<constructor-arg ref="sequenceNumberGenerator" />		
	</bean>
	
	<bean id="sequenceNumberGenerator" name="sequenceNumberGenerator"
		class="com.codesimplify.springcore.examples.SequenceNumberGenerator">
	</bean>
</beans>
Spring Framework – Dependency Injection Example

Leave a Reply

Your email address will not be published. Required fields are marked *