Skip to content
Related Articles

Related Articles

Spring – Setter Injection with Non-String Map

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • 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




// Java Program to Illustrate Company Class
   
package com.geeksforgeeks.org;
   
// Importing required classes
import com.geeksforgeeks.org.Address;
import com.geeksforgeeks.org.Employee;
import java.util.*;
import java.util.Map.Entry;
   
// Class
public 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.geeksforgeeks.org;
   
// Importing required classes
import com.geeksforgeeks.org.Address;
   
// Class
public 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.geeksforgeeks.org;
   
// Class
public 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.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




// Java Program to Illustrate Application Class
   
package com.geeksforgeeks.org;
   
// importing required classes
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;
   
// Application class
// Main class
public 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]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!