We will see below how to inject dependency object through the Constructor Injection.
package com.bk.spring.test;
public class CourseBean {
private String name;
private String duration;
private Double fee;
public CourseBean(String name, String duration, Double fee){
this.name = name;
this.duration = duration;
this.fee = fee;
}
public String getCourseDetails(){
return "Course " + name + " has " + duration + " duration with fee " + fee;
}
}
package com.bk.spring.test;
public class StudentBean {
private int id;
private String name;
private String emailId;
private CourseBean courseBean;
private double fee;
StudentBean() {
System.out.println("default constructor");
}
StudentBean(String name) {
this.name = name;
}
StudentBean(int id) {
this.id = id;
}
StudentBean(int id, String name, String emailId, CourseBean courseBean, double fee) {
this.id = id;
this.name = name;
this.emailId = emailId;
this.courseBean = courseBean;
this.fee = fee;
}
public void getStudentDetails() {
System.out.println("Id is " + this.id + " \n name is " + name
+ " \n email id is " + emailId + "\n" + courseBean.getCourseDetails());
}
}
<?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">
<constructor-arg value="001" />
<constructor-arg value="krishna" />
<constructor-arg value="Krishna@abc.com" />
<constructor-arg>
<ref bean="courseBean"/>
</constructor-arg>
<constructor-arg value="2018" />
</bean>
<bean name="courseBean" class="com.bk.spring.test.CourseBean">
<constructor-arg value="Java"/>
<constructor-arg value="90 Days"/>
<constructor-arg value="2018"/>
</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 CourseBean {
private String name;
private String duration;
private Double fee;
public CourseBean(String name, String duration, Double fee){
this.name = name;
this.duration = duration;
this.fee = fee;
}
public String getCourseDetails(){
return "Course " + name + " has " + duration + " duration with fee " + fee;
}
}
package com.bk.spring.test;
public class StudentBean {
private int id;
private String name;
private String emailId;
private CourseBean courseBean;
private double fee;
StudentBean() {
System.out.println("default constructor");
}
StudentBean(String name) {
this.name = name;
}
StudentBean(int id) {
this.id = id;
}
StudentBean(int id, String name, String emailId, CourseBean courseBean, double fee) {
this.id = id;
this.name = name;
this.emailId = emailId;
this.courseBean = courseBean;
this.fee = fee;
}
public void getStudentDetails() {
System.out.println("Id is " + this.id + " \n name is " + name
+ " \n email id is " + emailId + "\n" + courseBean.getCourseDetails());
}
}
<?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">
<constructor-arg value="001" />
<constructor-arg value="krishna" />
<constructor-arg value="Krishna@abc.com" />
<constructor-arg>
<ref bean="courseBean"/>
</constructor-arg>
<constructor-arg value="2018" />
</bean>
<bean name="courseBean" class="com.bk.spring.test.CourseBean">
<constructor-arg value="Java"/>
<constructor-arg value="90 Days"/>
<constructor-arg value="2018"/>
</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