Spring Setter Injection

Spring Framework allows to inject the dependency by setter method also. The subelement of is used for setter injection.

There are following values injected through setter injection.

  • Primitive Values

  • Dependent Object

  • Collection values

Here we will see an example to inject Primitive values using Setter injection.

JbkEmployee.java

package com.javabykiran.springioc.setterinjection;
public class JbkEmployee {
    String name;
    String city;
    String state;
    int salary;

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String getCity() {
	return city;
    }

    public void setCity(String city) {
	this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
	this.state = state;
    }

    public int getSalary() {
	return salary;
    }

    public void setSalary(int salary) {
	this.salary = salary;
    }

    public void display() {
	System.out.println("Name >> " + name);
	System.out.println("city >> " + city);
	System.out.println("state >> " + state);
	System.out.println("salary >> " + salary);
    }   
}

ApplicationContext.java

<?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: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.xsd">

	<bean id="jbkemployee" class="com.javabykiran.springioc.setterinjection.JbkEmployee">         
            <property name="name" value="JavaByKiran"></property>
            <property name="city" value="Pune"></property>
            <property name="state" value="Maharashtra"></property>
            <property name="salary" value="15000"></property>
	</bean>
</beans>

JbkTest.java

package com.javabykiran.springioc.setterinjection;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JbkTest {
    public static void main(String[] args) {
	ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("applicationContext.xml");
	JbkEmployee emp = cpac.getBean("jbkemployee", JbkEmployee.class);
	emp.display();
	cpac.close();
    }
}

Spring Framework provides following elements to inject collection values inside element.

  • list

  • map

  • set

Collection elements can be primitive or object collections. Here we’re taking example of JbkQuestion class which has multiple answers.

JbkQuestion.java
Question class has collection of String for multiple answers.

package com.javabykiran.springioc.setterinjection.list;
import java.util.List;
public class JbkQuestion {
    private int id;
    private String question;
    private List<String> answers;

    public int getId() {
        return id;
    }

    public void setId(int id) {
	this.id = id;
    }

    public String getQuestion() {
	return question;
    }

    public void setQuestion(String question) {
	this.question = question;
    }

    public List<String> getAnswers() {
	return answers;
    }

    public void setAnswers(List<String> answers) {
	this.answers = answers;
    }
}

applicationContext.xml

<?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: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.xsd">

    <bean id="jbkquestion" class="com.javabykiran.springioc.setterinjection.list.JbkQuestion">
	<property name="id" value="1"></property>
        <property name="question" value="What is Spring"></property>
	<property name="answers">
            <list>
		<value>Spring is Framework</value>
		<value>Spring is Platform</value>
            </list>
	</property>
    </bean>
</beans>

JbkTest.java

package com.javabykiran.springioc.setterinjection.list;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JbkTest {
    public static void main(String[] args) {
	ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("applicationContext.xml");
	JbkQuestion ques = cpac.getBean("jbkquestion", JbkQuestion.class);
	List<String> answers = ques.getAnswers();
	System.out.println("Question: " + ques.getQuestion());
	for (String ans : answers) {
	    System.out.println("Answers: " + ans);
	}
	cpac.close();
    }
}

Spring Framework allows to inject primitive or Object references with collection. There are three XML elements available for this.

  • < list >

  • < set >

  • < map >

Syntax For Primitive values Collection.

Here is syntax to define collection of primitive values.

<[list,set,map]>
<value>Value here<value>
</[list,set,map]>

Syntax For Object values Collection.

Here is syntax to define collection of object values.

<[list,set,map]>
<ref bean=”bean id”/>
</[list,set,map]>

In following program, we are taking the example of JbkCustomer and JbkAddress where Customer is one or addresses are more.

JbkCustomer.java

package com.javabykiran.springioc.setterinjection.list;
import java.util.Iterator;
import java.util.List;

public class JbkCustomer {
    private String name;
    private String email;
    private List<JbkAddress> addressList;

    public JbkCustomer(String name, String email, List<JbkAddress> addressList) {
	super();
	this.name = name;
	this.email = email;
	this.addressList = addressList;
    }

    public String getName() {
	return name;
    }
    public void setName(String name) {
	this.name = name;
    }

    public String getEmail() {
	return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<JbkAddress> getAddressList() {
	return addressList;
    }

    public void setAddressList(List<JbkAddress> addressList) {
	this.addressList = addressList;
    }

    public void display() {
	System.out.println("name >> " + name + " email >> " + email);
	Iterator<JbkAddress> itr = addressList.iterator();
        while (itr.hasNext()) {
            JbkAddress address = itr.next();
            System.out.println("JbkAddress >> city >> "+address.getCity() + " state >> " + address.getState() + " zipcode >> " + address.getZipcode());
	}
    }
}

JbkAddress.java

package com.javabykiran.springioc.setterinjection.list;
public class JbkAddress {
    private String city;
    private String state;
    private int zipcode;

    public String getCity() {
	return city;
    }

    public void setCity(String city) {
	this.city = city;
    }

    public String getState() {
	return state;
    }

    public void setState(String state) {
	this.state = state;
    }

    public int getZipcode() {
	return zipcode;
    }

    public void setZipcode(int zipcode) {
	this.zipcode = zipcode;
    }
}

JbkAddress.java

<?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: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.xsd">

    <bean id="jbkaddress1" class="com.javabykiran.springioc.setterinjection.list.JbkAddress">
	<property name="city" value="Nagpur"></property>
	<property name="state" value="Maharashtra"></property>
	<property name="zipcode" value="440001"></property>
    </bean>

    <bean id="jbkaddress2" class="com.javabykiran.springioc.setterinjection.list.JbkAddress">
	<property name="city" value="Pune"></property>
	<property name="state" value="Maharashtra"></property>
	<property name="zipcode" value="441021"></property>
    </bean>

    <bean id="jbkcustomer" class="com.javabykiran.springioc.setterinjection.list.JbkCustomer">
	<constructor-arg value="JavaByKiran"></constructor-arg>
	<constructor-arg value="JBK@enq.com"></constructor-arg>
	<constructor-arg>
            <list>
                <ref bean="jbkaddress1"></ref>
                <ref bean="jbkaddress2"></ref>
            </list>
	</constructor-arg>
    </bean>
</beans>

JbkTest.java

package com.javabykiran.springioc.setterinjection.list;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JbkTest {
    public static void main(String[] args) {
	ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("applicationContext.xml");
	JbkCustomer cust = cpac.getBean("jbkcustomer", JbkCustomer.class);
	cust.display();
	cpac.close();
    }
}

For Dependent Object injection we have HAS-A relation scenario. In our case there is dependency of Address object in Customer class. element has an attribute called ref to inject dependent object.

JbkCustomer.java

package com.javabykiran.springioc.setterinjection;
public class JbkCustomer {
    private String name;
    private String email;
    private JbkAddress address;

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String getEmail() {
	return email;
    }

    public void setEmail(String email) {
	this.email = email;
    }
        
    public JbkAddress getAddress() {
	return address;
    }

    public void setAddress(JbkAddress address) {
	this.address = address;
    }

    public void display() {
	System.out.println("name >> " + name + " email >> " + email);
	System.out.println("city >> " + address.getCity() + " state >> " + address.getState() + " zipcode >> " + address.getZipcode());
    }
}

JbkAddress.java

package com.javabykiran.springioc.setterinjection;
public class JbkAddress {
    private String city;
    private String state;
    private int zipcode;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
	return state;
    }

    public void setState(String state) {
	this.state = state;
    }

    public int getZipcode() {
	return zipcode;
    }

    public void setZipcode(int zipcode) {
	this.zipcode = zipcode;
    }
}

applicationContext.xml

<?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: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.xsd">

    <bean id="jbkaddress" class="com.javabykiran.springioc.setterinjection.JbkAddress">
	<property name="city" value="Pune"></property>
	<property name="state" value="Maharashtra"></property>
	<property name="zipcode" value="441021"></property>
    </bean>

    <bean id="jbkcustomer" class="com.javabykiran.springioc.setterinjection.JbkCustomer" autowire="byName">
	<property name="name" value="JavaByKiran"></property>
	<property name="email" value="JBK@enq.com"></property>
	<property name="address" ref="jbkaddress"></property>
    </bean>
</beans>

JbkTest.java

package com.javabykiran.springioc.setterinjection;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JbkTest {
    public static void main(String[] args) {
	ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext("applicationContext.xml");
	JbkCustomer cust = cpac.getBean("jbkcustomer", JbkCustomer.class);
	cust.display();
	cpac.close();
    }
}