In this Spring uses setter methods of a bean to provide its dependencies. This is done by using <property> tag in the xml configuration file. This is also known as setter-based dependency injection.
Suppose we have a Student class as:
In this class we have three dependencies (member variables) namely- age, name and course. 'course' is of type Course class which is given below:
The above Course class has two dependencies namely -name and courseId.
To provide these dependencies of Student and Course class following code will be used in ApplicationContext.xml:
In the above code we have used <property> tags which will use the setter methods of corresponding property.
Note while injecting value for course property we have used ref attribute instead of value attribute. This is because course refers to id of another bean rather than a simple value.
Unlike constructor-injection here order of properties is not important. So instead of
We can also write:
To run above code write following test class and run it as:
test.java
Output:
Suppose we have a Student class as:
package di.setter;
public class Student{
int age;
String name;
Course course;
Student(){
name = "Garry";
age = 24;
}
public void setAge(int age){
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setCourse(Course course){
this.course = course;
}
void showDetails(){
System.out.println("Student Name="+name+" Age="+age);
System.out.println("Course Details:");
System.out.println(" Name="+course.name);
System.out.println(" CourseId="+course.courseId);
}
}
public class Student{
int age;
String name;
Course course;
Student(){
name = "Garry";
age = 24;
}
public void setAge(int age){
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setCourse(Course course){
this.course = course;
}
void showDetails(){
System.out.println("Student Name="+name+" Age="+age);
System.out.println("Course Details:");
System.out.println(" Name="+course.name);
System.out.println(" CourseId="+course.courseId);
}
}
In this class we have three dependencies (member variables) namely- age, name and course. 'course' is of type Course class which is given below:
package di.setter;
public class Course{
String name;
int courseId;
public void setName(String name){
this.name = name;
}
public void setCourseId(int courseId){
this.courseId = courseId;
}
}
public class Course{
String name;
int courseId;
public void setName(String name){
this.name = name;
}
public void setCourseId(int courseId){
this.courseId = courseId;
}
}
The above Course class has two dependencies namely -name and courseId.
To provide these dependencies of Student and Course class following code will be used in ApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="student1" class="di.setter.Student">
<property name="name" value="Alex"/>
<property name="age" value="23"/>
<!-- Injecting reference to other bean -->
<property name="course" ref="course1"/>
</bean>
<bean id="course1" class="di.setter.Course">
<property name="name" value="Computers"/>
<property name="courseId" value="12345"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="student1" class="di.setter.Student">
<property name="name" value="Alex"/>
<property name="age" value="23"/>
<!-- Injecting reference to other bean -->
<property name="course" ref="course1"/>
</bean>
<bean id="course1" class="di.setter.Course">
<property name="name" value="Computers"/>
<property name="courseId" value="12345"/>
</bean>
</beans>
In the above code we have used <property> tags which will use the setter methods of corresponding property.
Note while injecting value for course property we have used ref attribute instead of value attribute. This is because course refers to id of another bean rather than a simple value.
Unlike constructor-injection here order of properties is not important. So instead of
<bean id="course1" class="di.setter.Course">
<property name="name" value="Computers"/>
<property name="courseId" value="12345"/>
</bean>
<property name="name" value="Computers"/>
<property name="courseId" value="12345"/>
</bean>
We can also write:
<bean id="course1" class="di.setter.Course">
<property name="courseId" value="12345"/>
<property name="name" value="Computers"/>
</bean>
<property name="courseId" value="12345"/>
<property name="name" value="Computers"/>
</bean>
To run above code write following test class and run it as:
test.java
package di.setter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student stud = (Student) appContext.getBean("student1");
stud.showDetails();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Student stud = (Student) appContext.getBean("student1");
stud.showDetails();
}
}
Output:
Student Name=Alex Age=23
Course Details:
Name=Computers
CourseId=12345
Course Details:
Name=Computers
CourseId=12345
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment