Tutorials

Setter Injection


SI Object dependency


Invoking the object dependency is very important to understand as this is what you will be doing most of the time in real world projects.

We can easily pass an object or multiple objects as an argument using property tag in xml configuaration file to achieve the setter injection

 

Given below is an example showing the way dependent object is injected using setter injection.

 

Java POJO - Subjects.java

This class object will be invoked as dependency using setter injection in Student.java class 

public class Subjects {

	private String subjectName;
	private String subjectID;
	
	public String getSubjectName() {
		return subjectName;
	}

	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}

	public String getSubjectID() {
		return subjectID;
	}

	public void setSubjectID(String subjectID) {
		this.subjectID = subjectID;
	}

	//overriding toString()
	public String toString(){
		return subjectName+ " "+subjectID;
	}
}

Java POJO - Student.java

This class has dependency on Subject.java class.


public class Student {
	private String name;
	private String stream;
	private Subjects subjects; //dependent object	 
	
	public String getName() {
		return name;
	}

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

	public String getStream() {
		return stream;
	}

	public void setStream(String stream) {
		this.stream = stream;
	}

	public Subjects getSubjects() {
		return subjects;
	}

	public void setSubjects(Subjects subjects) {  //injecting dependency
		this.subjects = subjects;
	}

	//display details
	public void displayStudentInfo(){
		System.out.println("Name :"+ name + "    "+ "Stream :"+ " " +stream);
		System.out.println("Subject details: "+ subjects);
	}

}

XML configuration - appContext.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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  


<bean id="studentBean" class="Student">  
<property name="name" value="Mukesh Tripathi"></property>
<property name="stream" value="ECE"></property>
<property name="subjects" ref="subjectBean">
</property>
</bean>

<bean id="subjectBean" class="Subjects">  
<property name="subjectName" value="Data Structure"></property>
<property name="subjectID" value="DS101"></property>
</bean>

</beans>

Test class - TestSpringClass.java

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

public class TestSpringClass {

	public static void main(String[] args) {		
		
		ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
		
		Student student = (Student) context.getBean("studentBean");
		student.displayStudentInfo();
	}

}

OUTPUT

Name :Mukesh Tripathi    Stream : ECE
Subject details: Data Structure DS101

 



Please Share this page
Views : 22
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!