📜  Spring – 非字符串集合的 Setter 注入

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

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

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

Java
// Java Program to Illustrate Company Class
 
package com.geeksforgeeks.org;
 
// Importing required classes
import java.util.*;
 
// 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;
    }
 
    // method
    public String getCompanyName() { return companyName; }
 
    public List getEmployees()
    {
        return employees;
    }
 
    // Method
    public void display()
    {
        System.out.println("Company: " + companyName);
        System.out.println("Empoyees:");
 
        // Iterating over using for each loop
        for (Employee employee : employees) {
            System.out.println(employee.toString());
        }
    }
}


Java
// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
 
// Class
class Employee {
 
    // Class data members
    private String name;
    private String employeeID;
    private String department;
 
    // Method
    public String getName() { return name; }
 
    // Setter
    public void setName(String name) { this.name = name; }
 
    // Getter
    public String getEmployeeID() { return employeeID; }
 
    // Setter
    public void setEmployeeID(String employeeID)
    {
        this.employeeID = employeeID;
    }
 
    // Getter
    public String getDepartment() { return department; }
 
    // Setter
    public void setDepartment(String department)
    {
        this.department = department;
    }
 
    // Method
    // Overriding toString() method of String class
    @Override public String toString()
    {
        return ("[Name: " + name
                + ", Employee Id: " + employeeID
                + ", Department: " + department + "]");
    }
}


XML
 
 
   
     
         
         
         
     
     
     
         
         
         
     
     
         
         
        
            
                  
                
             
         
     


Java
// Java Program to Illustrate Application Class
 
package com.geeksforgeeks.org;
 
// Importing required classes from respective packages
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 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 Company class
        Company c = (Company)factory.getBean("company");
 
        // Calling display() method inside main() method
        c.display();
    }
}



B. 员工。Java

Employee 类具有以下属性:

  • 姓名
  • 员工ID
  • 部门

Java

// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
 
// Class
class Employee {
 
    // Class data members
    private String name;
    private String employeeID;
    private String department;
 
    // Method
    public String getName() { return name; }
 
    // Setter
    public void setName(String name) { this.name = name; }
 
    // Getter
    public String getEmployeeID() { return employeeID; }
 
    // Setter
    public void setEmployeeID(String employeeID)
    {
        this.employeeID = employeeID;
    }
 
    // Getter
    public String getDepartment() { return department; }
 
    // Setter
    public void setDepartment(String department)
    {
        this.department = department;
    }
 
    // Method
    // Overriding toString() method of String class
    @Override public String toString()
    {
        return ("[Name: " + name
                + ", Employee Id: " + employeeID
                + ", Department: " + department + "]");
    }
}


C.applicationContext.xml

它是一个包含所有 bean 的配置文件。

XML

 
 
   
     
         
         
         
     
     
     
         
         
         
     
     
         
         
        
            
                  
                
             
         
     
 

D. 主要的。Java

这是我们的 application(main) 类,它将调用 Company 类的 display() 方法

Java

// Java Program to Illustrate Application Class
 
package com.geeksforgeeks.org;
 
// Importing required classes from respective packages
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 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 Company class
        Company c = (Company)factory.getBean("company");
 
        // Calling display() method inside main() method
        c.display();
    }
}


输出:

Company: GeeksForGeeks
Employees:
[Name: John, Employee ID: 211, Department: Penetration testing]
[Name: Sam, Employee ID: 212, Department: Ethical hacking]