How to use Has-A relationship using Dependency Injection?

In the previous article, we have seen What is Dependency Injection you can check it here What is Dependency Injection in Spring. In this article, we will go one step ahead and will understand How to implement the HAS-A relationship using DI.

What is the HAS-A relationship?

HAS-A is a simple term that means when one Class has a reference of another class. for eg. If there is a class Person who has a reference for Mobile Class.

In Spring, we will Inject the dependency using the Constructor Injection and Setter Method Injection that we will see using the following example.

Example of Dependency Injection using Constructor in HAS-A relationship

Create a Spring Project Go to File> New > Other > Search maven > Select Maven Project > Next > Search Filter org.apche.maven.archetypes/webapp > Next > Enter Group Id & Archetype id > Finish.

  • Create a Class Person having a name attribute/property and a reference of Mobile class.

Person.java

package com.codedec.di;

public class Person {

  private String name, city; // This is the dependency of Person class
  private Mobile mobile;

  public Person(String name, String city, Mobile mobile) {
    this.name = name;
    this.city = city;
    this.mobile = mobile;
  }

  public void show() {
    System.out.println(
        "Name of person is :: " + name + "\n City of person is :: " + city + "\n  Mobile Info  is  " + mobile);
  }

}
  • Create a class Mobile with mobileNo, mobileName, mobileMemory attribute, and constructor.
  • Add a toString() method to represent the String representation of an Object.

Mobile.java

package com.codedec.di;

public class Mobile {

  private long mobileNo;
  private String mobileName;
  private String mobileMemory;

  public Mobile(long mobileNo, String mobileName, String mobileMemory) {
    this.mobileNo = mobileNo;
    this.mobileName = mobileName;
    this.mobileMemory = mobileMemory;
  }

  @Override
  public String toString() {
    return " [mobileNo=" + mobileNo + ", mobileName=" + mobileName + ", mobileMemory=" + mobileMemory + "]";
  }

}
  • Create a beans.xml file with <constructor-arg> property.
  • In this file, we have used the ref attribute to refer to another object i.e Mobile Object m1.
  • In this way, we are invoking the dependent object as a constructor argument.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="m1" class="com.codedec.di.Mobile">
    <constructor-arg name="mobileNo" value="9595959595"></constructor-arg>
    <constructor-arg name="mobileName" value="Nokia"></constructor-arg>
    <constructor-arg name="mobileMemory" value="32GB"></constructor-arg>
  </bean>

  <bean id="p1" class="com.codedec.di.Person">
    <constructor-arg name="name" value="Nicolas"></constructor-arg>
    <constructor-arg name="city" value="Amsterdam"></constructor-arg>
    <constructor-arg>
      <ref bean="m1" />
    </constructor-arg>
  </bean>
</beans>
  • Create an App.java and create the Object of Application Context, get the bean from it and call the show() method.

App.java

package com.codedec.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
     Person person= context.getBean("p1",Person.class);
     person.show();
  }
}

Output

Name of person is :: Nicolas
 City of person is :: Amsterdam
  Mobile Info  is   [mobileNo=9595959595, mobileName=Nokia, mobileMemory=32GB]

Thus, In this way, we inject the Dependent Object using Constructor Injection.

Example of Dependency Injection using Setter Method in HAS-A relationship

Create a Spring Project Go to File> New > Other > Search maven > Select Maven Project > Next > Search Filter org.apche.maven.archetypes/webapp > Next > Enter Group Id & Archetype id > Finish.

  • Create a Class Person having a name attribute/property, a reference of Mobile class, and a setter method.

Person.java

package com.codedec.di;

public class Person {

  private String name, city; // This is the dependency of Person class
  private Mobile mobile;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public Mobile getMobile() {
    return mobile;
  }

  public void setMobile(Mobile mobile) {
    this.mobile = mobile;
  }

  public void show() {
    System.out.println(
        "Name of person is :: " + name + "\n City of person is :: " + city + "\n  Mobile Info  is  " + mobile);
  }

}
  • Create a class Mobile with mobileNo, mobileName, mobileMemory attribute, and setter Method.
  • Add a toString() method to represent the String representation of an Object.

Mobile.java

package com.codedec.di;

public class Mobile {

  private long mobileNo;
  private String mobileName;
  private String mobileMemory;

  public long getMobileNo() {
    return mobileNo;
  }

  public void setMobileNo(long mobileNo) {
    this.mobileNo = mobileNo;
  }

  public String getMobileName() {
    return mobileName;
  }

  public void setMobileName(String mobileName) {
    this.mobileName = mobileName;
  }

  public String getMobileMemory() {
    return mobileMemory;
  }

  public void setMobileMemory(String mobileMemory) {
    this.mobileMemory = mobileMemory;
  }

  @Override
  public String toString() {
    return " [mobileNo=" + mobileNo + ", mobileName=" + mobileName + ", mobileMemory=" + mobileMemory + "]";
  }

}
  • Create a beans.xml file with <property> element
  • This property element will invoke the setter method.
  • In this file, we have used the ref attribute inside the property element that will refer to another object i.e Mobile Object ‘m1’

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="m1" class="com.codedec.di.Mobile">
    <property name="mobileNo" value="9595959595"></property>
    <property name="mobileName" value="Nokia"></property>
    <property name="mobileMemory" value="32GB"></property>
  </bean>

  <bean id="p1" class="com.codedec.di.Person">
    <property name="name" value="Nicolas"></property>
    <property name="city" value="Amsterdam"></property>
    <property name="mobile" ref="m1"></property>
  </bean>
</beans>
  • Create an App.java and create the Object of Application Context, get the bean from it and call the show() method.

App.java

package com.codedec.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
     Person person= context.getBean("p1",Person.class);
     person.show();
  }
}

Output

Name of person is :: Nicolas
 City of person is :: Amsterdam
  Mobile Info  is   [mobileNo=9595959595, mobileName=Nokia, mobileMemory=32GB]

Thus, this is the way we inject dependent objects using Setter Injection.

Thus, we have seen both ways of invoking dependent objects using the Constructor and Setter Method.