We will see below how to inject dependency object through the Setter Injection.
package com.bk.spring.test;
public class StudentBean {
private int id;
private String name;
private String emailId;
private CourseBean courseBean;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public CourseBean getCourseBean() {
return courseBean;
}
public void setCourseBean(CourseBean courseBean) {
this.courseBean = courseBean;
}
public void getStudentDetails() {
System.out.println("Id is " + this.id + " \n name is " + name
+ " \n email id is " + emailId + "\n" + courseBean.getCourseDetails());
}
}
package com.bk.spring.test;
public class CourseBean {
private String name;
private String duration;
private Double fee;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public Double getFee() {
return fee;
}
public void setFee(Double fee) {
this.fee = fee;
}
public String getCourseDetails(){
return "Course " + name + " has " + duration + " duration with fee " + fee;
}
}
<?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 name="student" class="com.bk.spring.test.StudentBean">
<property name="id" value="1"></property>
<property name="name" value="Krishna"></property>
<property name="emailId" value="Krishna@abc.com"></property>
<property name="courseBean" ref="courseBean"></property>
</bean>
<bean name="courseBean" class="com.bk.spring.test.CourseBean">
<property name="name" value="Java"></property>
<property name="duration" value="90Days"></property>
<property name="fee" value="2018"></property>
</bean>
</beans>
package com.bk.spring.test;
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 SpringConDITest {
public static void main(String a[]) {
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(res);
StudentBean bean = (StudentBean) factory.getBean("student");
bean.getStudentDetails();
}
}
Output:
Id is 1
name is Krishna
email id is Krishna@abc.com
Course Java has 90Days duration with fee 2018.0
package com.bk.spring.test;
public class StudentBean {
private int id;
private String name;
private String emailId;
private CourseBean courseBean;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public CourseBean getCourseBean() {
return courseBean;
}
public void setCourseBean(CourseBean courseBean) {
this.courseBean = courseBean;
}
public void getStudentDetails() {
System.out.println("Id is " + this.id + " \n name is " + name
+ " \n email id is " + emailId + "\n" + courseBean.getCourseDetails());
}
}
package com.bk.spring.test;
public class CourseBean {
private String name;
private String duration;
private Double fee;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public Double getFee() {
return fee;
}
public void setFee(Double fee) {
this.fee = fee;
}
public String getCourseDetails(){
return "Course " + name + " has " + duration + " duration with fee " + fee;
}
}
<?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 name="student" class="com.bk.spring.test.StudentBean">
<property name="id" value="1"></property>
<property name="name" value="Krishna"></property>
<property name="emailId" value="Krishna@abc.com"></property>
<property name="courseBean" ref="courseBean"></property>
</bean>
<bean name="courseBean" class="com.bk.spring.test.CourseBean">
<property name="name" value="Java"></property>
<property name="duration" value="90Days"></property>
<property name="fee" value="2018"></property>
</bean>
</beans>
package com.bk.spring.test;
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 SpringConDITest {
public static void main(String a[]) {
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(res);
StudentBean bean = (StudentBean) factory.getBean("student");
bean.getStudentDetails();
}
}
Output:
Id is 1
name is Krishna
email id is Krishna@abc.com
Course Java has 90Days duration with fee 2018.0
No comments:
Post a Comment