Spring – Setter Injection with Non-String Map
Last Updated :
11 Mar, 2022
Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods. In Setter Dependency Injection(SDI) the dependency will be injected with the help of setters and getters methods. A bean-configuration file is used to set DI as SDI in the bean. For this, the property to be set with the SDI is declared under the <property> tag in the bean-config file. A Collection in java is a group of individual objects. Spring framework provides us facility of Setter injection using the following Collections:
Implementation
The map will have both key and value as non-strings. Key will be Employee which has the following fields:
- Name
- Employee ID
- Department
Value will be Address which has the following parameters:
- House No.
- Pincode
- State
- Country
A. Company.java
A company has a list of employees.
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
import com.geeksforgeeks.org.Employee;
import java.util.*;
import java.util.Map.Entry;
public class Company {
private Map<Employee, Address> employees;
public void display()
{
for (Map.Entry<Employee, Address> entry :
employees.entrySet()) {
System.out.println(
"Employee Data ->"
+ entry.getKey().toString() + " Address ->"
+ entry.getValue().toString());
}
}
}
|
B. Employee.java
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
public class Employee {
private String name;
private String employeeID;
private String department;
public Employee(String name, String employeeID,
String department)
{
this .name = name;
this .employeeID = employeeID;
this .department = department;
}
public String toString()
{
return ( "[" + name + "," + employeeID + ","
+ department + "]" );
}
}
|
C. Address.java
Java
package com.geeksforgeeks.org;
public class Address {
private String houseNo;
private String pincode;
private String state;
private String country;
public Address(String houseNo, String pincode,
String state, String country)
{
super ();
this .houseNo = houseNo;
this .pincode = pincode;
this .state = state;
this .country = country;
}
public String toString()
{
return "[" + houseNo + "," + pincode + "," + state
+ "," + country + "]" ;
}
}
|
D. applicationContext.xml
Configuration file for our project.
Java
<?xml version= "1.0" encoding= "UTF-8" ?>
<beans
xsi:schemaLocation="http:
http:
<bean id= "employee1" class = "com.geeksforgeeks.org.Employee" >
<property name= "name" value= "Sahil" ></property>
<property name= "employeeID" value= "101" ></property>
<property name= "department" value= "Game development" ></property>
</bean>
<bean id= "address1" class = "com.geeksforgeeks.org.Address" >
<property name= "houseNo" value= "2" ></property>
<property name= "pincode" value= "110111" ></property>
<property name= "state" value= "Bihar" ></property>
<property name= "country" value= "India" ></property>
</bean>
<bean id= "company" class = "com.geeksforgeeks.org.Company" >
<map>
<entry key-ref= "employee1" value-ref= "address1" ></entry>
</map>
</property>
</bean>
</beans>
|
E. Test.java
Main file for running our project.
Java
package com.geeksforgeeks.org;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args)
{
Resource resource = new ClassPathResource(
"applicationContext.xml" );
BeanFactory factory = new XmlBeanFactory(resource);
Company c = (Company)factory.getBean( "company" );
c.display();
}
}
|
Output:
Employee Data -> [Sahil, 101, Game development], Address -> [2, 110111, Bihar, India]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...