Spring – 带有集合的构造函数注入
在构造函数注入中,依赖注入将在构造函数的帮助下注入。现在要将依赖注入设置为bean中的构造函数依赖注入(CDI),它是通过bean配置文件完成的。为此,要在构造函数依赖注入中设置的属性在
例子:
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.IGeek;
public class GFG {
// The object of the interface IGeek
IGeek geek;
// Constructor to set the CDI
GFG(IGeek geek) { this.geek = geek; }
}
XML
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.IGeek;
import java.util.List;
public class Employee {
private String name;
private String employeeID;
private String department;
private List address;
public Employee(List address)
{
this.address = (List)address;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getemployeeID() { return employeeID; }
public void setemployeeID(String employeeID)
{
this.employeeID = employeeID;
}
public String getdepartment() { return department; }
public void setdepartment(String department)
{
this.department = department;
}
public List getAddress() { return address; }
public void display()
{
System.out.println("Name: " + getName());
System.out.println("Employee ID: "
+ getEmployeeID());
System.out.println("Department: "
+ getDepartment());
System.out.println("Address: " + getAddress());
}
}
XML
Gurugram
Haryana
India
Java
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;
public class Test {
public static void main(String[] args)
{
Resource resource = new ClassPathResource(
"applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Employee e = (Employee)factory.getBean("employee");
e.display();
}
}
在 bean-config 文件中设置 CDI:
XML
构造函数注入与集合
Spring 框架为我们提供了通过 Spring 应用程序中的构造函数注入集合值的工具。
- 列表
- 放
- 地图
例子:
1) 雇员。Java
创建一个员工类:
Java
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.IGeek;
import java.util.List;
public class Employee {
private String name;
private String employeeID;
private String department;
private List address;
public Employee(List address)
{
this.address = (List)address;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getemployeeID() { return employeeID; }
public void setemployeeID(String employeeID)
{
this.employeeID = employeeID;
}
public String getdepartment() { return department; }
public void setdepartment(String department)
{
this.department = department;
}
public List getAddress() { return address; }
public void display()
{
System.out.println("Name: " + getName());
System.out.println("Employee ID: "
+ getEmployeeID());
System.out.println("Department: "
+ getDepartment());
System.out.println("Address: " + getAddress());
}
}
2) applicationContext.xml
要定义列表,请使用 constructor-arg 的列表元素。
XML
Gurugram
Haryana
India
3) 测试。Java
测试弹簧应用程序:
Java
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;
public class Test {
public static void main(String[] args)
{
Resource resource = new ClassPathResource(
"applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Employee e = (Employee)factory.getBean("employee");
e.display();
}
}
输出:
Name: Ram
Employee ID: 101
Department: Software testing
Address: [Gurugram, Haryana, India]