📌  相关文章
📜  Spring - 非字符串映射的构造函数注入

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

Spring - 非字符串映射的构造函数注入

在构造函数注入中,依赖注入将在构造函数的帮助下注入。现在要将依赖注入设置为bean中的构造函数依赖注入(CDI),它是通过bean配置文件完成的。为此,要在构造函数依赖注入中设置的属性在标签下声明豆配置文件。

带有集合的构造函数注入

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

  • 列表
  • 地图

实现:在下面的示例中,我们将看到使用 Map 的构造函数注入。该地图将具有作为非字符串的键和值。键将是Employee ,它具有以下字段:

  1. 姓名
  2. 员工ID
  3. 部门

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

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

A.公司。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;
 
    // Constructor
    public Company(Map employees)
    {
        // This keyword refers to current instance itself
        this.employees = 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 + "]";
    }
}


XML
 
 
   
 
     
     
     
 
   
 
     
     
     
      
 
   
 
     
         
              
         
     
 
   


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
        Employee c = (Employee)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

XML

 
 
   
 
     
     
     
 
   
 
     
     
     
      
 
   
 
     
         
              
         
     
 
   

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

输出:

Employee Data -> [Ram, 101, software testing], Address -> [110/4, 128933, Delhi, India]