Spring – 使用 Map 的构造函数注入
在构造函数注入中,依赖注入将在构造函数的帮助下注入。现在要将依赖注入设置为bean中的构造函数依赖注入(CDI),它是通过bean配置文件完成的。为此,要在构造函数依赖注入中设置的属性在
插图:
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.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;
}
}
XML
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 employees;
// Constructor
public Company(Map employees)
{
// this keyword refers to current instance itself
this.employees = employees;
}
// Method
public void display()
{
for (Map.Entry entry :
employees.entrySet()) {
System.out.println(
"Employee Id ->" + entry.getKey() + ","
+ " Department->" + entry.getValue());
}
}
}
XML
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();
}
}
在 bean-config 文件中设置 CDI
XML
带映射的构造函数注入
Spring 框架为我们提供了通过 Spring 应用程序中的构造函数注入集合值的工具。
- 列表
- 放
- 地图
实现:在下面的示例中,我们将看到使用 Map 的构造函数注入。该地图将具有作为字符串的键和值。
A.公司。Java
一家公司可以有多名员工。每个员工都有一个员工 ID 和部门。 'employees' 映射将员工 ID 存储为key ,各个部门将存储为values 。
例子:
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 employees;
// Constructor
public Company(Map employees)
{
// this keyword refers to current instance itself
this.employees = employees;
}
// Method
public void display()
{
for (Map.Entry 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.
例子:
XML
C.测试。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();
}
}
输出:
Employee ID -> 101, Department -> Software development
Employee ID -> 102, Department -> Software testing
Employee ID -> 103, Department -> Security