Setter
Injection With Dependent Object And Collection Example
Dependency
Injection By Setter method
Can
inject the dependency by setter method also.
The
<property>
sub-element of <bean> is
used for setter injection.
<bean
id="studentBean"
class="com.Student">
<property
name="studentRef"><value>STD001</value></property>
<property
name="fName"><value>Sanjeeva</value></property>
<property
name="lName"><value>Pathirana</value></property>
<property
name="age"><value>30</value></property>
</bean>
Setter
Injection with Dependent object
For
this injection we can use for HAS-A relationship occurrences.
E.g.
Student HAS-A Academic Year
<bean
id="studentBean"
class="com.Student">
...
<property
name="academicInfo"
ref="academicInfoBean"> </property>
</bean>
<bean
id="academicInfoBean"
class="com.AcademicInfo">
...
</bean>
Setter
Injection with Collection Example
Can
inject collection values by using setter method.
There
are 3 elements which use with <property> element.
1.
list
<property
name="extraCurriculums">
<list>
<value>Annual
Debt</value>
<value>Rugby
Tournament</value>
</list>
</property>
<bean
id="grade1Bean"
class="com.Grade">
<property
name="gradeLevel"><value>1st
Class</value></property>
<property
name="gradeDescription"><value>Over
3.5 GPA</value></property>
</bean>
<list>
<ref
bean="grade1Bean"/>
<ref
bean="grade2Bean"/>
</list>
2.
map
<property
name="semesterInfoMap">
<map>
<entry
key="1st
Semester - Starts" value="Jan
2014"></entry>
</map>
</property>
<property
name="subjectInfoMap">
<map>
<entry
key-ref="subject1Bean"
value-ref="lecturer1Bean"></entry>
</map>
</property>
Find
the example below.
Steps
as follows:
- Create Student.java, Subject.java, Grade.java, Lecturer.java, AcademicInfo.java classes
- Create ApplicationContext.xml
- Create executable class called DisplayStudentInfo.java
1.
Create Student.java, Subject.java, Grade.java, Lecturer.java,
AcademicInfo.java classes
package
com;
import
java.io.Serializable;
public
class
Subject implements
Serializable {
/**
*
*/
private
static
final
long
serialVersionUID
= 1L;
private
String subjectCode;
private
String subject;
public
String displaySubject(){
return
"\tSubject Code : "
+ subjectCode
+ "\tSubject : "
+ subject;
}
public
String getSubjectCode() {
return
subjectCode;
}
public
void
setSubjectCode(String subjectCode) {
this.subjectCode
= subjectCode;
}
public
String getSubject() {
return
subject;
}
public
void
setSubject(String subject) {
this.subject
= subject;
}
}
package
com;
import
java.io.Serializable;
public
class
Lecturer implements
Serializable {
/**
*
*/
private
static
final
long
serialVersionUID
= 1L;
private
String lectureInCharge;
private
String lectureEmail;
public
String lectureInfo(){
return
"\tLecture In Charge : "
+ lectureInCharge
+ "\te-mail :"
+ lectureEmail;
}
public
String getLectureInCharge() {
return
lectureInCharge;
}
public
void
setLectureInCharge(String lectureInCharge) {
this.lectureInCharge
= lectureInCharge;
}
public
String getLectureEmail() {
return
lectureEmail;
}
public
void
setLectureEmail(String lectureEmail) {
this.lectureEmail
= lectureEmail;
}
}
package
com;
import
java.io.Serializable;
public
class
AcademicInfo implements
Serializable {
/**
*
*/
private
static
final
long
serialVersionUID
= 1L;
private
String studentYear;
private
int
noOfSemisters;
private
int
noOfCompulsorySubject;
private
int
noOfOptionalSubject;
public
String displayAcademicInfo(){
return
"\n\tStudent Year : "
+ studentYear
+ "\n\tNo. of Semesters : "
+ noOfSemisters
+ "\n\tCompulsory Subjects : "
+ noOfCompulsorySubject
+ "\n\tOptional Subjects : "
+ noOfOptionalSubject;
}
public
String getStudentYear() {
return
studentYear;
}
public
void
setStudentYear(String studentYear) {
this.studentYear
= studentYear;
}
public
int
getNoOfSemisters() {
return
noOfSemisters;
}
public
void
setNoOfSemisters(int
noOfSemisters) {
this.noOfSemisters
= noOfSemisters;
}
public
int
getNoOfCompulsorySubject() {
return
noOfCompulsorySubject;
}
public
void
setNoOfCompulsorySubject(int
noOfCompulsorySubject) {
this.noOfCompulsorySubject
= noOfCompulsorySubject;
}
public
int
getNoOfOptionalSubject() {
return
noOfOptionalSubject;
}
public
void
setNoOfOptionalSubject(int
noOfOptionalSubject) {
this.noOfOptionalSubject
= noOfOptionalSubject;
}
}
package
com;
import
java.io.Serializable;
public
class
Grade implements
Serializable {
/**
*
*/
private
static
final
long
serialVersionUID
= 1L;
private
String gradeLevel;
private
String gradeDescription;
public
String getGradeLevel() {
return
gradeLevel;
}
public
void
setGradeLevel(String gradeLevel) {
this.gradeLevel
= gradeLevel;
}
public
String getGradeDescription() {
return
gradeDescription;
}
public
void
setGradeDescription(String gradeDescription) {
this.gradeDescription
= gradeDescription;
}
public
String displayGrades(){
return
"\tLevel : "
+ gradeLevel
+ "\n\tDescription : "
+ gradeDescription
+ "\n";
}
}
package
com;
import
java.io.Serializable;
import
java.util.Iterator;
import
java.util.List;
import
java.util.Map;
import
java.util.Map.Entry;
import
java.util.Set;
public
class
Student implements
Serializable {
/**
*
*/
private
static
final
long
serialVersionUID
= 1L;
private
String studentRef;
private
String fName;
private
String lName;
private
int
age;
private
AcademicInfo academicInfo;
private
List<String> extraCurriculumList;
private
List<Grade> gradeList;
private
Map<String, String> semesterInfoMap;
private
Map<Subject, Lecturer> subjectInfoMap;
public
void
displayStudentDetails(){
System.out.println("Student
Ref\t:\t"+studentRef);
System.out.println("fName\t\t:\t"+fName);
System.out.println("lName\t\t:\t"+lName);
System.out.println("age\t\t:\t"+age);
System.out.println("\nAcademis
Info.\t:\t"+academicInfo.displayAcademicInfo());
System.out.println("\nSemester
Information : ");
Set<Entry<String,
String>> setSI = semesterInfoMap.entrySet();
Iterator<Entry<String,
String>> itrSI = setSI.iterator();
while(itrSI.hasNext()){
System.out.println("\t"
+ itrSI.next().getKey() + " "
+ itrSI.next().getValue() );
}
System.out.println("\nSemester
Information : ");
Set<Entry<Subject,
Lecturer>> setSub = subjectInfoMap.entrySet();
Iterator<Entry<Subject,
Lecturer>> subSI = setSub.iterator();
while(subSI.hasNext()){
Entry<Subject,
Lecturer> entry = subSI.next();
Subject
subject = entry.getKey();
Lecturer
lecturer = entry.getValue();
System.out.println(".\t"
+ subject.displaySubject() + " "
+ lecturer.lectureInfo() );
}
System.out.println("\nGrades
: ");
Iterator<Grade>
itrGrades = gradeList.iterator();
while(itrGrades.hasNext()){ System.out.println(itrGrades.next().displayGrades());
}
System.out.println("\nExtra
Curricular Activities");
Iterator<String>
itrExCur = extraCurriculumList.iterator();
while(itrExCur.hasNext()){
System.out.println("\t*
" + itrExCur.next());
}
}
public
String getStudentRef() {
return
studentRef;
}
public
void
setStudentRef(String studentRef) {
this.studentRef
= studentRef;
}
public
String getfName() {
return
fName;
}
public
void
setfName(String fName) {
this.fName
= fName;
}
public
String getlName() {
return
lName;
}
public
void
setlName(String lName) {
this.lName
= lName;
}
public
int
getAge() {
return
age;
}
public
void
setAge(int
age) {
this.age
= age;
}
public
AcademicInfo getAcademicInfo() {
return
academicInfo;
}
public
void
setAcademicInfo(AcademicInfo academicInfo) {
this.academicInfo
= academicInfo;
}
public
List<String> getExtraCurriculumList() {
return
extraCurriculumList;
}
public
void
setExtraCurriculumList(List<String> extraCurriculumList) {
this.extraCurriculumList
= extraCurriculumList;
}
public
List<Grade> getGradeList() {
return
gradeList;
}
public
void
setGradeList(List<Grade> gradeList) {
this.gradeList
= gradeList;
}
public
Map<String, String> getSemesterInfoMap() {
return
semesterInfoMap;
}
public
void
setSemesterInfoMap(Map<String, String> semesterInfoMap) {
this.semesterInfoMap
= semesterInfoMap;
}
public
Map<Subject, Lecturer> getSubjectInfoMap() {
return
subjectInfoMap;
}
public
void
setSubjectInfoMap(Map<Subject, Lecturer> subjectInfoMap) {
this.subjectInfoMap
= subjectInfoMap;
}
}
2.
Create 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean
id="studentBean"
class="com.Student">
<property
name="studentRef"><value>STD001</value></property>
<property
name="fName"><value>Sanjeeva</value></property>
<property
name="lName"><value>Pathirana</value></property>
<property
name="age"><value>30</value></property>
<property
name="academicInfo"
ref="academicInfoBean"></property>
<property
name="extraCurriculumList">
<list>
<value>Annual
Debt</value>
<value>Rugby
Tournament</value>
<value>Poem
competition</value>
</list>
</property>
<property
name="gradeList">
<list>
<ref
bean="grade1Bean"/>
<ref
bean="grade2Bean"/>
</list>
</property>
<property
name="semesterInfoMap">
<map>
<entry
key="1st
Semester - Starts" value="Jan
2014"></entry>
<entry
key="1st
Semester - Ends" value="Apr
2014"></entry>
<entry
key="2nd
Semester - Starts" value="Jun
2014"></entry>
<entry
key="2nd
Semester - Ends" value="Oct
2014"></entry>
</map>
</property>
<property
name="subjectInfoMap">
<map>
<entry
key-ref="subject1Bean"
value-ref="lecturer1Bean"></entry>
<entry
key-ref="subject2Bean"
value-ref="lecturer2Bean"></entry>
</map>
</property>
</bean>
<bean
id="academicInfoBean"
class="com.AcademicInfo">
<property
name="studentYear"><value>2nd
Year</value></property>
<property
name="noOfSemisters"><value>02</value></property>
<property
name="noOfCompulsorySubject"><value>08</value></property>
<property
name="noOfOptionalSubject"><value>04</value></property>
</bean>
<bean
id="grade1Bean"
class="com.Grade">
<property
name="gradeLevel"><value>1st
Class</value></property>
<property
name="gradeDescription"><value>Over
3.5 GPA</value></property>
</bean>
<bean
id="grade2Bean"
class="com.Grade">
<property
name="gradeLevel"><value>2nd
Upper</value></property>
<property
name="gradeDescription"><value>Over
3.0 GPA</value></property>
</bean>
<bean
id="subject1Bean"
class="com.Subject">
<property
name="subjectCode"
value="SUB0001"></property>
<property
name="subject"
value="Software
Engineering"></property>
</bean>
<bean
id="subject2Bean"
class="com.Subject">
<property
name="subjectCode"
value="SUB0002"></property>
<property
name="subject"
value="Network
Engineering"></property>
</bean>
<bean
id="lecturer1Bean"
class="com.Lecturer">
<property
name="lectureInCharge"
value="Thinuli
Pathirana"></property>
<property
name="lectureEmail"
value="thinuli@gmail.com"></property>
</bean>
<bean
id="lecturer2Bean"
class="com.Lecturer">
<property
name="lectureInCharge"
value="Sandamali
Silva"></property>
<property
name="lectureEmail"
value="sandamali@gmail.com"></property>
</bean>
</beans>
3.
Create DisplayAuthorInformation.java
package
com;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.core.io.ClassPathResource;
import
org.springframework.core.io.Resource;
public
class
DisplayStudentInfo {
/**
* @p aram
args
*/
public
static
void
main(String[] args) {
DisplayStudentInfo
studentInfo = new
DisplayStudentInfo();
studentInfo.doExecute();
}
private
void
doExecute(){
Resource
resource = new
ClassPathResource("resource/ApplicationContext.xml");
BeanFactory
beanFactory = new
XmlBeanFactory(resource);
Student
student = (Student)beanFactory.getBean("studentBean");
student.displayStudentDetails();
}
}
Out
put as follows:
Student
Ref : STD001
fName : Sanjeeva
lName : Pathirana
age : 30
Academis
Info. :
Student
Year : 2nd Year
No.
of Semesters : 2
Compulsory
Subjects : 8
Optional
Subjects : 4
Semester
Information :
1st
Semester - Starts Apr 2014
2nd
Semester - Starts Oct 2014
Semester
Information :
. Subject
Code : SUB0001 Subject : Software Engineering Lecture In Charge :
Thinuli Pathirana e-mail :thinuli@gmail.com
. Subject
Code : SUB0002 Subject : Network Engineering Lecture In Charge :
Sandamali Silva e-mail :sandamali@gmail.com
Grades
:
Level
: 1st Class
Description
: Over 3.5 GPA
Level
: 2nd Upper
Description
: Over 3.0 GPA
Extra
Curricular Activities
*
Annual Debt
*
Rugby Tournament
*
Poem competition