Spring – Setter Dependency Injection

Here I show how spring container during run-time injects the value to a variable with reference from spring configuration file. We use setters and getters to be created for spring container to achieve that.

Setter Dependency Injection


public class Circle {

@Description("The value to this variable will be injected "
+ "by Spring container during runtime with reference from configuration file")
private String type;

public String getType() {
return type;

public void setType(String type) {
this.type = type;

public void draw() {
System.out.println(getType() + " circle drawn!!");

Spring.xml configuration file

<!– Setter Dependency Injection Demo –>
<bean id=”circle” class=”com.saravanansivaji.spring.Circle”>
<property name=”typevalue=”Eclipse”></property>

Application class SpringDI.java

public class SpringDI {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
@Description("Setter Dependency Injection Demo")
Circle circle = (Circle) context.getBean("circle");


Eclipse circle drawn!!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: