📜  Spring – 带有依赖对象的 Setter 注入

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

Spring – 带有依赖对象的 Setter 注入

依赖注入是Spring IOC(Inversion of Control)提供的主要功能。 Spring-Core 模块负责通过 Constructor 或 Setter 方法注入依赖项。

在 Setter 依赖注入(SDI)中,依赖将在 setter 和 getter 方法的帮助下注入。 bean 配置文件用于将 DI 设置为 bean 中的 SDI。为此,要使用 SDI 设置的属性在 bean-config 文件中的 标记下声明。

具有依赖对象的 Setter 注入

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

实现:以下示例演示了具有依赖对象的 setter 注入。

A.员工。Java

每个员工都有以下属性:

  • 姓名
  • 员工ID
  • 部门
  • 地址(从属对象)

示例:员工类

Java
// Java Program to Illustrate Employee Class
 
package com.geeksforgeeks.org;
 
// Class
class Employee {
 
    // Class member members
    private String name;
    private String employeeID;
    private String department;
    private Address address;
 
    // Setter
    public void setName(String name) { this.name = name; }
 
    // Setter
    public void setemployeeID(String employeeID)
    {
        this.employeeID = employeeID;
    }
 
    // Setter
    public void setdepartment(String department)
    {
        this.department = department;
    }
 
    // Setter
    public void setAddress(Address address)
    {
        this.address = address;
    }
 
    // Getter
    public String getName() { return name; }
 
    // Getter
    public String getemployeeID() { return employeeID; }
 
    // Getter
    public String getdepartment() { return department; }
 
    // Getter
    public Address getAddress() { return address; }
 
    // Method
    public void display()
    {
        // Print statements
        System.out.println("Name: " + getName());
        System.out.println("Employee ID: "
                           + getEmployeeID());
        System.out.println("Department: "
                           + getDepartment());
        System.out.println("Address: "
                           + getAddress().toString());
    }
}


Java
package com.geeksforgeeks.org;
 
class Address {
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
     
    public Address(String houseNo, String pincode, String state, String country) {
        super();
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state;
        this.country = country;
    }
     
    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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 Employee class
        Employee e = (Employee)factory.getBean("employee");
 
        // Calling print() method inside main() method
        e.display();
    }
}


B.地址。Java

地址具有以下属性:

  • 门牌号码
  • PIN码
  • 状态
  • 国家

例子:

Java

package com.geeksforgeeks.org;
 
class Address {
    private String houseNo;
    private String pincode;
    private String state;
    private String country;
     
    public Address(String houseNo, String pincode, String state, String country) {
        super();
        this.houseNo = houseNo;
        this.pincode = pincode;
        this.state = state;
        this.country = country;
    }
     
    public String toString() {
        return "["+ houseNo + "," + pincode + "," + state + "," + country + "]";
    }
 
}

C.applicationContext.xml

我们将使用property元素的ref属性来指定地址bean 在员工bean 中的引用。

XML

 
 
   
     
         
         
         
         
     
   
     
         
         
        
         
     
   
 

D.应用文件(Java)

该类将调用 Employee 类的display()方法。

例子:

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.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 Employee class
        Employee e = (Employee)factory.getBean("employee");
 
        // Calling print() method inside main() method
        e.display();
    }
}

输出:

Name: Ram
Employee ID: 1001
Department: Software development
Address: [110/4, 121212, Delhi, India]