📜  Spring - 非字符串集合的构造函数注入

📅  最后修改于: 2022-05-13 01:55:07.034000             🧑  作者: Mango

Spring - 非字符串集合的构造函数注入

依赖注入是Spring IOC(Inversion of Control)提供的主要功能。 Spring-Core 模块负责通过 Constructor 或 Setter 方法注入依赖项。在构造函数注入中,依赖注入将在构造函数的帮助下注入。现在要将依赖注入设置为bean中的构造函数依赖注入(CDI),它是通过bean配置文件完成的。为此,要在构造函数依赖注入中设置的属性在标签下声明豆配置文件。

Spring 框架为我们提供了通过 Spring 应用程序中的构造函数注入集合值的工具。可以在 标记内使用以下集合。

  • 列表
  • 地图

实现:在以下示例中,我们将看到使用非字符串集合的构造函数注入。

一家公司。Java

一家公司可以有多名员工。这里的Company类与Employee关系。 Company 类将包含 Employee 类的实例。因此,Employee 对象将是依赖对象。我们将使用这个依赖对象的列表来演示非字符串(依赖对象)集合(列表)的构造函数注入。

Java
// Java Program to Illustrate Company Class
 
package com.geeksforgeeks.org;
 
// Importing required classes
import java.util.*;
 
// Class
class Company {
 
    private List employees;
 
    // Constructor
    public Company(List employees)
    {
        this.employees = employees;
    }
 
    // Method
    public void display()
    {
        Iterator it = employees.iterator();
 
        // Holds true until there is single element
        // remaining inside object
        while (it.hasNext()) {
 
            System.out.println(it.next().toString());
        }
    }
}


Java
// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
 
// Class
class Employee {
 
    // Class data member
    private String name;
    private String employeeID;
    private String department;
 
    // Constructor
    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
    public String toString()
    {
        return ("[" + name + ", " + employeeID + ", "
                + department + "]");
    }
}


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(Main) Class
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 Company class
        Company c = (Company)factory.getBean("company");
 
        // Calling display() method inside main() method
        c.display();
    }
}



B. 员工。Java

每个员工都有一个姓名、员工 ID 和部门。

Java

// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
 
// Class
class Employee {
 
    // Class data member
    private String name;
    private String employeeID;
    private String department;
 
    // Constructor
    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
    public String toString()
    {
        return ("[" + name + ", " + employeeID + ", "
                + department + "]");
    }
}


C. 应用上下文。Java

XML

 
 
   
     
         
        
         
     
     
     
         
        
         
    
     
     
         
             
                 
                 
             
          
    
 

D. 测试。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(Main) Class
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 Company class
        Company c = (Company)factory.getBean("company");
 
        // Calling display() method inside main() method
        c.display();
    }
}

输出:

[Sam, 101, Software testing]
[John, 102, Software development]