2) @Autowired on Fields
Following example shows how to use @Autowired on the fields of a class.
Student.java
package examples.springanno;
import org.springframework.beans.factory.annotation.Autowired;
public class Student {
private String name;
private String address;
@Autowired 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;
}
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;
@Autowired 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;
}
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 {
private String courseName;
public void setCourseName(String courseName){
this.courseName = courseName;
}
public String getCourseName(){
return courseName;
}
}
public class Course {
private String courseName;
public void setCourseName(String courseName){
this.courseName = courseName;
}
public String getCourseName(){
return courseName;
}
}
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();
}
}
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"/>
<!--Note that we are not injecting value of third property 'course' here because
this property is annotated @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"/>
<!--Note that we are not injecting value of third property 'course' here because
this property is annotated @Autowired.
<property name="course" ref="course"/>
-->
</bean>
<bean id="course" class="examples.springanno.Course">
<property name="courseName" value="Computer Applications"/>
</bean>
</beans>
Output:
Student name : abc
Student Address : xyz
Course Details:
Course Name : Computer Applications
Student Address : xyz
Course Details:
Course Name : Computer Applications
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