Spring Setter injection for reference bean
In most of the cases, one of the attributes of a class(A) is a class(B) by itself. In such cases, setting values for class A would involve setting values for Class B in turn. Then, reference to the bean ID can be passed while setting the bean using Setter injection.
The basic working of spring framework is now clear. Now, we can keep doing modifications in the existing base project to further build on our understanding. Let us take up a practical scenario for the next step. One of the attributes of a class <Candidate> is another class<Address>. The value of the class Candidate depends on the value of the Class Address. i.e., there is a dependency here.
Step 1:
Create a new classes Candidate, Address(POJOs) and define the attributes as above. For this example, simply create the setter and getter methods for each attributes, through which their values will be injected
File : Candidate.java
package com.simpleCodeStuffs; public class Candidate { private String name; private int age; private Address addrs; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddrs() { return addrs; } public void setAddrs(Address addrs) { this.addrs = addrs; } }
File :Address.java
package com.simpleCodeStuffs; public class Address { private String doorNo; private String street; private String area; public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getDoorNo() { return doorNo; } public void setDoorNo(String doorNo) { this.doorNo = doorNo; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } }
Step 2:
Create the main class.
File : MainClass.java
package com.simpleCodeStuffs; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainClass { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Beans.xml"); Candidate can = (Candidate) context.getBean("candidate"); Address add = (Address) context.getBean("address"); System.out.println("Name : " + can.getName()); System.out.println("Age: " + can.getAge()); System.out.println("Address : " + can.getAddrs().getDoorNo()); System.out.println("\t " + add.getStreet()); System.out.println("\t " + add.getArea()); } }
Make sure that all these xml files are loaded properly in your project classpath.
File : Beans.xml
A bean is created corresponding to both the classes(Candidate and Address).Make changes here to add multiple property tags corresponding to the various attributes present inside each of the classes. One major difference here is, the property Address under Candidate does not have a direct value for itself, but is rather a separate bean. Hence, instead of providing value, provide ref for this property. The value provided in ref will be nothing but the beanID of the Address class.
We have used setter injection to set values for the properties. Hence, we use the tag property and give the value to be set (which is passed to the corresponding setter methods) as the value correspondingly.
Run it :
The output on running the above code (setter injection or constructor injection will be the same) will be :-