📜  Spring – 带有依赖对象的构造函数注入

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

Spring – 带有依赖对象的构造函数注入

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

例子:

Java
package com.geeksforgeeks.org;
 
import com.geeksforgeeks.org.IGeek;
 
public class GFG {
 
    // The object of the interface IGeek
    IGeek geek;
 
    // Constructor to set the CDI
    GFG(IGeek geek) { this.geek = geek; }
}


XML

  
    
        
            
        
    
      


          


Java
// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
 
// Class
public class Employee {
    private String name;
    private String employeeID;
    private String department;
 
    // Here 'address' is an dependent object.
    private Address address;
 
    // Parameterised constructor
    public Employee(String name, String employeeID,
                    String department, Address address)
    {
        // this keyword refers to current instance itself
        this.name = name;
        this.employeeID = employeeID;
        this.department = department;
        this.address = (Address)address;
    }
 
    // Method
    public void display()
    {
        System.out.println("Name: " + name);
        System.out.println("Employee ID: " + employeeID);
        System.out.println("Department: " + department);
        System.out.println("Address: " + address);
    }
}


Java
// Java Program to Illustrate Address Class
 
package com.geeksforgeeks.org;
 
public class Address {
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
 
    // Constructor of Address Class
    public Address(String houseNo, String pincode,
                   String state, String country)
    {
        // Super keyword refers to parent class
        super();
        // This keyword refers to current instance itself
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state;
        this.country = country;
    }
 
    // Method
    public String toString()
    {
        return "[" + houseNo + "," + pincode + "," + state
            + "," + country + "]";
    }
}


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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
 
// Main(Application) class
public class Test {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating 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");
        e.display();
    }
}



在 bean-config 文件中设置 CDI 如下 XML 文件所示,如下所示:

XML


  
    
        
            
        
    
      


          

带有依赖对象的构造函数注入

如果我们的 spring 应用程序的类之间存在关系,那么我们创建依赖对象的实例,也称为包含对象。创建依赖对象的实例后,我们将其作为主类容器的参数传递。示例:如果员工有地址,那么地址类的实例就是依赖对象。

A. 员工。Java

Java

// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.Address;
 
// Class
public class Employee {
    private String name;
    private String employeeID;
    private String department;
 
    // Here 'address' is an dependent object.
    private Address address;
 
    // Parameterised constructor
    public Employee(String name, String employeeID,
                    String department, Address address)
    {
        // this keyword refers to current instance itself
        this.name = name;
        this.employeeID = employeeID;
        this.department = department;
        this.address = (Address)address;
    }
 
    // Method
    public void display()
    {
        System.out.println("Name: " + name);
        System.out.println("Employee ID: " + employeeID);
        System.out.println("Department: " + department);
        System.out.println("Address: " + address);
    }
}


B. 地址。Java

Java

// Java Program to Illustrate Address Class
 
package com.geeksforgeeks.org;
 
public class Address {
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
 
    // Constructor of Address Class
    public Address(String houseNo, String pincode,
                   String state, String country)
    {
        // Super keyword refers to parent class
        super();
        // This keyword refers to current instance itself
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state;
        this.country = country;
    }
 
    // Method
    public String toString()
    {
        return "[" + houseNo + "," + pincode + "," + state
            + "," + country + "]";
    }
}

C.applicationContext.xml

XML

 
 
   
       
         
         
         
           
     
   
     
         
        
        
         
         
         
     
   

D. 测试。Java

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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
 
// Main(Application) class
public class Test {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating 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");
        e.display();
    }
}

输出:

Name: Ram
Employee ID: 101
Department: Software testing
Address: [110/4, 123092, Delhi, India]