📜  Spring – 使用非字符串映射的 Setter 注入

📅  最后修改于: 2022-05-13 01:55:19.536000             🧑  作者: 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 注入工具:

  • 列表
  • 地图

执行

该地图将具有作为非字符串的键和值。键将是 Employee ,它具有以下字段:

  • 姓名
  • 员工ID
  • 部门

值将是具有以下参数的地址:

  • 房屋编号
  • PIN码
  • 状态
  • 国家

一家公司。Java

一家公司有一份员工名单。

Java
// Java Program to Illustrate Company Class
   
package com.geeksforgeeks.org;
   
// Importing required classes
import com.geeksforgeeks.org.Address;
import com.geeksforgeeks.org.Employee;
import java.util.*;
import java.util.Map.Entry;
   
// Class
public class Company {
   
    // Class member variable
    private Map employees;
   
    // Method
    public void display()
    {
        // Iterating over Map using for each loop
        for (Map.Entry entry :
             employees.entrySet()) {
   
            // Print statement
            System.out.println(
                "Employee Data ->"
                + entry.getKey().toString() + " Address ->"
                + entry.getValue().toString());
        }
    }
}


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


Java
// Java Program to Illustrate Address Class
   
package com.geeksforgeeks.org;
   
// Class
public class Address {
   
    // Class data members
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
   
    // Constructor
    public Address(String houseNo, String pincode,
                   String state, String country)
    {
        super();
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state;
        this.country = country;
    }
   
    // Method
    // Overriding toString() method
    public String toString()
    {
        return "[" + houseNo + "," + pincode + "," + state
            + "," + country + "]";
    }
}


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


B. 员工。Java

Java

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

C. 地址。Java

Java

// Java Program to Illustrate Address Class
   
package com.geeksforgeeks.org;
   
// Class
public class Address {
   
    // Class data members
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
   
    // Constructor
    public Address(String houseNo, String pincode,
                   String state, String country)
    {
        super();
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state;
        this.country = country;
    }
   
    // Method
    // Overriding toString() method
    public String toString()
    {
        return "[" + houseNo + "," + pincode + "," + state
            + "," + country + "]";
    }
}

D.applicationContext.xml

我们项目的配置文件。

Java

  
  
    
      
        
        
        
      
        
        
        
        
        
      
  
      
        
        
        
        
      
    

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

输出:

Employee Data -> [Sahil, 101, Game development], Address -> [2, 110111, Bihar, India]