Autowire by Name

Student.java


package com.sridhar.autowired;

public class Student {

private ExamMark student1;
private ExamMark student2;
private ExamMark student3;

public ExamMark getStudent1() {
return student1;
}
public void setStudent1(ExamMark student1) {
this.student1 = student1;
}
public ExamMark getStudent2() {
return student2;
}
public void setStudent2(ExamMark student2) {
this.student2 = student2;
}
public ExamMark getStudent3() {
return student3;
}
public void setStudent3(ExamMark student3) {
this.student3 = student3;
}

public void displayMarks()
{
System.out.println(getStudent1().getStudentId()+ " Marks are: "+ getStudent1().getSub1() +" "+ getStudent1().getSub2());
System.out.println(getStudent2().getStudentId()+ " Marks are: "+ getStudent2().getSub1() +" "+ getStudent2().getSub2());
System.out.println(getStudent3().getStudentId()+ " Marks are: "+ getStudent3().getSub1() +" "+ getStudent3().getSub2());
}

}

ExamMark.java

package com.sridhar.autowired;

public class ExamMark {
	private String studentId;
	private int Sub1;
	private int Sub2;

	public ExamMark(String studentId)
	{
		this.studentId = studentId;

	}

	public ExamMark( String studentId, int sub1, int sub2)
	{
		this.studentId = studentId;
		this.Sub1 = sub1;
		this.Sub2 = sub2;
	}

	public int getSub1() {
		return Sub1;
	}

	public void setSub1(int sub1) {
		Sub1 = sub1;
	}

	public int getSub2() {
		return Sub2;
	}

	public void setSub2(int sub2) {
		Sub2 = sub2;
	}

	public String getStudentId() {
		return studentId;
	}

	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}

}

springAutoWire.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="student" class="com.sridhar.autowired.Student" autowire="byName"></bean>

	<bean id="student1" class="com.sridhar.autowired.ExamMark">
		<!-- <property name="studentId" value="CSE1001"></property> -->
		<constructor-arg value="CSE1002" />
		<constructor-arg value="89" />
		<constructor-arg value="95" />

	</bean>
	<bean id="student2" class="com.sridhar.autowired.ExamMark">
		<!-- <property name="studentId" value="CSE1001"></property> -->
		<constructor-arg value="CSE1003" />
		<constructor-arg value="50" />
		<constructor-arg value="80" />

	</bean>
	<bean id="student3" class="com.sridhar.autowired.ExamMark">
		<!-- <property name="studentId" value="CSE1001"></property> -->
		<constructor-arg value="CSE1004" />
		<constructor-arg value="90" />
		<constructor-arg value="98" />

	</bean>

</beans>

TestStudentAutoWire.java

package com.sridhar.autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudentAutoWire {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext ctx = new ClassPathXmlApplicationContext("springAutoWire.xml");
		Student student = (Student)ctx.getBean("student");
		student.displayMarks();
	}
}

Leave a comment