Spring – 带有集合的 Setter 注入
依赖注入是Spring IOC(Inversion of Control)提供的主要功能。 Spring-Core 模块负责通过 Constructor 或 Setter 方法注入依赖项。在 Setter 依赖注入(SDI)中,依赖将在 setter 和 getter 方法的帮助下注入。 bean 配置文件用于将 DI 设置为 bean 中的 SDI。为此,要使用 SDI 设置的属性在 bean-config 文件中的
Java中的 Collection 是一组单独的对象。 Spring 框架使用以下集合为我们提供了 Setter 注入工具:
- 列表
- 地图
- 放
执行:
一家公司。Java
A company has a list of employees.
Java
// Java program to Illustrate Company Class
package com.geeksforgeeks.org;
// Importing required classes
import java.util.*;
// Company Class
class Company {
// Class data members
private String companyName;
private List employees;
// Setter
public void setCompanyName(String companyName)
{
this.companyName = companyName;
}
// Setter
public void setEmployees(List employees)
{
this.employees = employees;
}
// Getter
public String getCompanyName() { return companyName; }
// Getter
public List getEmployees() { return employees; }
// Method
public void display()
{
System.out.println("Company: " + companyName);
System.out.println("Employee list: " + companyName);
// Iterating over using for each loop
for (String employee : employees) {
System.out.println("-" + employee);
}
}
}
XML
"John"
"Max"
"Sam"
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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
// Application (Main) Class
public class Main {
// Main driver method
public static void main(String[] args)
{
// Creating a new 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 e = (Employee)factory.getBean("employee");
// Calling display() method inside main() method
e.display();
}
}
B.applicationContext.xml
我们将使用属性元素进行 setter 注入。 property 属性的name元素将等于变量名称,而value元素将包含您要分配给该变量的值。
XML
"John"
"Max"
"Sam"
C. 主要的。Java
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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
// Application (Main) Class
public class Main {
// Main driver method
public static void main(String[] args)
{
// Creating a new 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 e = (Employee)factory.getBean("employee");
// Calling display() method inside main() method
e.display();
}
}
输出:
Company: GeeksForGeeks
Employee list:
-John
-Max
-Sam