Spring – Setter Injection with Non-String Map

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
// Java Program to Illustrate Company Class package com.zambiatek.com; // Importing required classesimport com.zambiatek.com.Address;import com.zambiatek.com.Employee;import java.util.*;import java.util.Map.Entry; // Classpublic class Company { // Class member variable private Map<Employee, Address> employees; // Method public void display() { // Iterating over Map using for each loop for (Map.Entry<Employee, Address> entry : employees.entrySet()) { // Print statement System.out.println( "Employee Data ->" + entry.getKey().toString() + " Address ->" + entry.getValue().toString()); } }} |
B. Employee.java
Java
// Java Program to Illustrate Employee Class package com.zambiatek.com; // Importing required classesimport com.zambiatek.com.Address; // Classpublic class Employee { // Class data members private String name; private String employeeID; private String department; public Employee(String name, String employeeID, String department) { // This keyword refers to current instance itself this.name = name; this.employeeID = employeeID; this.department = department; } // Method // Overriding toString() method public String toString() { return ("[" + name + "," + employeeID + "," + department + "]"); }} |
C. Address.java
Java
// Java Program to Illustrate Address Class package com.zambiatek.com; // Classpublic class Address { // Class data members private String houseNo; private String pincode; private String state; private String country; // Constructor public Address(String houseNo, String pincode, String state, String country) { super(); this.houseNo = houseNo; this.pincode = pincode; this.state = state; this.country = country; } // Method // Overriding toString() method 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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="employee1" class="com.zambiatek.com.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.zambiatek.com.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.zambiatek.com.Company"> <map> <entry key-ref="employee1" value-ref="address1"></entry> </map> </property> </bean> </beans> |
E. Test.java
Main file for running our project.
Java
// Java Program to Illustrate Application Class package com.zambiatek.com; // importing required classesimport org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource; // Application class// Main classpublic class Test { // Main driver method public static void main(String[] args) { // Creating a class path resource Resource resource = new ClassPathResource( "applicationContext.xml"); // Creating an object of Beanfactory class BeanFactory factory = new XmlBeanFactory(resource); // Creating an object of Employee class Company c = (Company)factory.getBean("company"); // Calling print() method inside main() method c.display(); }} |
Output:
Employee Data -> [Sahil, 101, Game development], Address -> [2, 110111, Bihar, India]



