Implementation of Constructor Injection with Non-String Map in Spring
Below is the implementation with class files of constructor injection with non-string map in a Spring application.
Company.java:
// Java Program to Illustrate Company Class
package com.w3wiki.org;
// Importing required classes
import com.w3wiki.org.Address;
import com.w3wiki.org.Employee;
import java.util.*;
import java.util.Map.Entry;
// Class
public class Company {
// Class member variable
private Map<Employee, Address> employees;
// Constructor
public Company(Map<Employee, Address> employees)
{
// This keyword refers to current instance itself
this.employees = 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());
}
}
}
- This Company class represents a Company entity with employees and their addresses stored as a map.
- The display() method iterates over the map entries, printing each employee’s data along with their corresponding address.
Employee.java:
// Java Program to Illustrate Employee Class
package com.w3wiki.org;
// Importing required classes
import com.w3wiki.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 + "]");
}
}
- This Employee class defines an Employee entity with attributes such as name, employee ID, and department.
- It includes a constructor to initialize these attributes and overrides the toString() method to return a string representation of the employee’s data.
Address.java:
// Java Program to Illustrate Address Class
package com.w3wiki.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 + "]";
}
}
- This Address class represents an Address entity with attributes such as house number, pincode, state, and country.
- It includes a constructor to initialize these attributes and overrides the toString() method to return a string representation of the address’s data.
applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="employee1" class="com.w3wiki.org.Employee">
<constructor-arg value="Ram"></constructor-arg>
<constructor-arg value="101"></constructor-arg>
<constructor-arg value="Software development"></constructor-arg>
</bean>
<bean id="address1" class="com.w3wiki.org.Address">
<constructor-arg value="110/4"></constructor-arg>
<constructor-arg value="128933"></constructor-arg>
<constructor-arg value="Delhi"></constructor-arg>
<constructor-arg value="India"></constructor-arg>
</bean>
<bean id="company" class="com.w3wiki.org.Company">
<constructor-arg>
<map>
<entry key-ref="employee1" value-ref="address1"></entry>
</map>
</constructor-arg>
</bean>
</beans>
Test.java:
// Java Program to Illustrate Application Class
package com.w3wiki.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 company = (Company)factory.getBean("company");
// Calling print() method inside main() method
company.display();
}
}
- This Test class demonstrates how to use Spring’s XML-based configuration to create beans and wire them together using dependency injection.
- It loads the bean definitions from the “applicationContext.xml” file and creates a BeanFactory.
- It retrieves the Company bean from the Spring container and call its display() method.
Output:
Employee Data -> [Ram, 101, software testing], Address -> [110/4, 128933, Delhi, India]
Spring – Constructor Injection with Non-String Map
In the Constructor Injection, the dependency injection will be injected with the help of constructors. Now to set the dependency injection as constructor dependency injection(CDI) in bean is done through the bean-configuration file. For this, the property to be set with the constructor dependency injection is declared under the <constructor-arg> tag in the bean-config file.
Constructor Injection with Collection
Spring frameworks provide us the facility to inject collection values via a constructor in our spring application. The following collections can be used inside the <constructor-arg> tag:
- list
- set
- map
Contact Us