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, it 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.
Illustration:
Java
package com.geeksforgeeks.org; import com.neveropen.tech.IGeek; public class GFG { // Creating an object of the interface IGeek IGeek geek; // Constructor to set the CDI GFG(IGeek geek) { // This keyword refers to current instance itself this .geek = geek; } } |
Setting the CDI in the bean-config File
XML
< beans xsi:schemaLocation="http://www.springframework.org/schema/beans < bean id = "GFG" class = "com.neveropen.tech.GFG" > < constructor-arg > < bean class = "com.neveropen.tech.impl.CsvGFG" /> </ constructor-arg > </ bean > < bean id = "CsvGFG" class = "com.neveropen.tech.impl.CsvGFG" /> < bean id = "JsonGFG" class = "com.neveropen.tech.impl.JsonGFG" /> </ beans > |
Constructor Injection with Map
Spring frameworks provide us the facility to inject collection values via constructor in our spring application. The following collections can be used inside the <constructor-arg> tag:
- list
- set
- map
Implementation: In the following example, we will see constructor injection with Map. The map will have both key and value as String.
A. Company.java
A company can have multiple employees. Each employee will have an employee ID and department. The ’employees’ map will store employee IDs as key and the respective departments will be stored as values.
Example:
Java
// Java Program to illustrate Company Class package com.geeksforgeeks.org; // Importing required classes import java.util.*; import java.util.Map.Entry; // Class public class Company { // Class member variables private Map<String, String> employees; // Constructor public Company(Map<String, String> employees) { // this keyword refers to current instance itself this .employees = employees; } // Method public void display() { for (Map.Entry<String, String> entry : employees.entrySet()) { System.out.println( "Employee Id ->" + entry.getKey() + "," + " Department->" + entry.getValue()); } } } |
B. applicationContext.xml
The entry attribute of map will be used to store key and value information.
Example:
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xsi:schemaLocation="http://www.springframework.org/schema/beans < bean id = "company" class = "com.neveropen.tech.Company" > < constructor-arg > < map > < entry key = "101" value = "Software development" ></ entry > < entry key = "102" value = "Software testing" ></ entry > < entry key = "103" value = "Security" ></ entry > </ map > </ constructor-arg > </ bean > </ beans > |
C. Test.java
Java
// Java Program to Illustrate Application Class 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; // 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 Employee c = (Employee)factory.getBean( "company" ); // Calling print() method inside main() method c.display(); } } |
Output:
Employee ID -> 101, Department -> Software development Employee ID -> 102, Department -> Software testing Employee ID -> 103, Department -> Security