In our previous discussions we have seen, how to create a sample spring project with maven and import it in Eclipse. In this article we will see how to configure dependency injection in spring usingsetter method.

Dependency required for spring IOC

Setup a spring project to test the example, if you are new to spring go through the following link to Setup a simple spring project:


		
			org.springframework
			spring-context
			${spring.version}
		


Spring DI using Setter Method

Lets say we have two classes Apple and Orange, we will try to inject an instance of Apple into Orange using spring DI. We will try to call a method of Apple in Orange class for the purpose we need an instance of Apple in Orange class:

1) Without DI old method

/src/main/java/com/tbNext/beans/Apple.java

package com.tbNext.beans;

public class Apple {
	public void printName() {
		System.out.println("Apple");
	}
}


/src/main/java/com/tbNext/beans/Orange.java

package com.tbNext.beans;

public class Orange {

	public void printApple() {
		Apple apple = new Apple();
		apple.printName();
	}
}

Ad we can see, to call Apple's printName() we have created an instance of Apple in Orange using new keyword. This is tightly coupled as Orange is creating Apple's instance.

2) Using DI injection

We can take leverage of Spring DI to manage instances through spring container, for doing this we have to first configure all the beans in spring-conf.xml like this:



	
	
		
	



Now we can call Apple's method in Orange like this:

/src/main/java/com/tbNext/beans/Apple.java

package com.tbNext.beans;

public class Apple {
	public void printName() {
		System.out.println("Apple");
	}
}

/src/main/java/com/tbNext/beans/Orange.java

package com.tbNext.beans;

public class Orange {

	Apple apple;

	// settter method
	public void setApple(Apple apple) {
		this.apple = apple;
	}

	public void printApple() {

		apple.printName();
	}
}

Test it: We can test the functionality, here is how to use it
/src/main/java/com/tbNext/App/Application.java

package com.tbNext.App;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tbNext.beans.Orange;

public class Application {
	public static void main(String args[]) {
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"spring-conf.xml");
		Orange orange = (Orange) applicationContext.getBean("orange");
		orange.printApple();
	}

}

As you can see, orange is not creating the instance of Apple, an instance of apple is being served by the container using setter method here. This way we can use spring DI using setter methods.

Hence to get an instance from the container using setter methiod we have to do 2 things.

1) Add bean entry in spring-conf.xml and pass a property of injecting object with name and reference.


	
		
	

2) Create an instance variable and its setter in calling class.
Apple apple;

	// settter method
	public void setApple(Apple apple) {
		this.apple = apple;
	}

That's it for this lesson, we have seen how to implement spring ioc using setter method with maven, in upcoming articles we will see more about spring and other technologies.
  • By Techburps.com
  • Oct 8, 2015
  • Spring Framework