1) @Autowired on Setter methods
Following example shows how to use @Autowired on Setter methods.
Student.java
Course.java
ApplicationContext.xml
MainApp.java
Output:
Note that in above code (in ApplicationContext.xml) we are providing values of only two fields of Student class namely - name and address. We are not providing value for third field - course. The value of this third field i.e course will be injected using @Autowired because we have annotated its setter method with @Autowired in the Student class.
@Autowired does [by type autowiring]. It will search configuration file for a bean whose type is same as that corresponding property on which @Autowired has been applied. If such matching bean found then it will inject it's reference to that property as its value.
Since our field course is of type Course, @Autowired will search xml configuration file for a bean which is of type Course and then inject its reference as value for third field of Student class.
Following example shows how to use @Autowired on Setter methods.
Student.java
package examples.springanno;
import org.springframework.beans.factory.annotation.Autowired;
public class Student {
private String name;
private String address;
private Course course;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
@Autowired
public void setCourse(Course c){
this.course = c;
}
public Course getCourse(){
return course;
}
public void displayInfo(){
System.out.println("Student name : "+name);
System.out.println("Student Address : "+address);
System.out.println(" Course Details:");
System.out.println(" Course Name : "+this.course.getCourseName());
}
}
import org.springframework.beans.factory.annotation.Autowired;
public class Student {
private String name;
private String address;
private Course course;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
@Autowired
public void setCourse(Course c){
this.course = c;
}
public Course getCourse(){
return course;
}
public void displayInfo(){
System.out.println("Student name : "+name);
System.out.println("Student Address : "+address);
System.out.println(" Course Details:");
System.out.println(" Course Name : "+this.course.getCourseName());
}
}
Course.java
package examples.springanno;
public class Course {
String courseName;
public void setCourseName(String courseName){
this.courseName = courseName;
}
public String getCourseName(){
return courseName;
}
}
public class Course {
String courseName;
public void setCourseName(String courseName){
this.courseName = courseName;
}
public String getCourseName(){
return courseName;
}
}
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"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- Enabling Spring to use @Autowired-->
<context:annotation-config/>
<bean id="student" class="examples.springanno.Student" >
<property name="name" value="abc"/>
<property name="address" value="xyz"/>
<!-- No need to write following line.This property will be automatically injected using @Autowired.
<property name="course" ref="course"/>
-->
</bean>
<bean id="course" class="examples.springanno.Course">
<property name="courseName" value="Computer Applications"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- Enabling Spring to use @Autowired-->
<context:annotation-config/>
<bean id="student" class="examples.springanno.Student" >
<property name="name" value="abc"/>
<property name="address" value="xyz"/>
<!-- No need to write following line.This property will be automatically injected using @Autowired.
<property name="course" ref="course"/>
-->
</bean>
<bean id="course" class="examples.springanno.Course">
<property name="courseName" value="Computer Applications"/>
</bean>
</beans>
MainApp.java
package examples.springanno;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Student student = (Student)ctx.getBean("student");
student.displayInfo();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Student student = (Student)ctx.getBean("student");
student.displayInfo();
}
}
Output:
Student name : abc
Student Address : xyz
Course Details:
Course Name : Computer Applications
Student Address : xyz
Course Details:
Course Name : Computer Applications
Note that in above code (in ApplicationContext.xml) we are providing values of only two fields of Student class namely - name and address. We are not providing value for third field - course. The value of this third field i.e course will be injected using @Autowired because we have annotated its setter method with @Autowired in the Student class.
@Autowired does [by type autowiring]. It will search configuration file for a bean whose type is same as that corresponding property on which @Autowired has been applied. If such matching bean found then it will inject it's reference to that property as its value.
Since our field course is of type Course, @Autowired will search xml configuration file for a bean which is of type Course and then inject its reference as value for third field of Student class.
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