Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring - Setter Injection using XML

We have seen Spring creating and managing beans/objects for you. Now, we will see how, by using setter methods spring is going to inject beans for you.

Say there is a class called 'Car' :


class Car {

  String brand;

  public String getBrand() {
   return brand;
  }

  public void setBrand(String brand) {
   this.brand = brand;
  }
}

Also let us define the application-context.xml



application-context.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
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean name = "car" class = "Car">
   <property name = "brand" value = "Hyundai"/>
  </bean>

</beans>


Just compare the contents of <bean ..> tag with the 'Car' class.

We have linked the 'Car' class in XML using the below code :


<bean name = "car" class = "Car">

Also the 'Car' class has a property named 'brand'.


<property name = "brand" value = "Hyundai"/>

On seeing the above line Spring goes to the Setter method of the 'brand' property of the 'Car' class and injects the value 'Hyundai' to 'brand'.


public void setBrand(String brand) {
  this.brand = brand;
}

Finally, in the main() method we define our application code to interact with spring.


import org.springframework.context.ApplicationContext;

public class FirstSpringApp {
  public static void main(String[] args) {

  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");

  Car car = (Car)applicationContext.getBean("car");

  System.out.println("The Car brand is : "+car.getBrand());

  }
}

A Complex Setter Injection

All the Cars rolls on Wheels. Now, let us say our 'Car' class holds a reference of a 'Wheel' class.


Car Class

class Car {

 Wheel wheel;

 public Wheel getWheel() {
   return wheel;
 }

 public void setWheel(Wheel wheel) {
   this.wheel = wheel;
 }
}

Wheel Class

class Wheel {

  String tyreType;

  public String getTyreType() {
   return tyreType;
 }

  public void setTyreType(String tyreType) {
   this.tyreType = tyreType;
  }

}

Now, since the 'Car' class is holding a reference of 'Wheel' . We will define them in 'application-context.xml'.


application-context.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
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean name = "car" class = "Car">
   <property name = "wheel" ref = "wheelReference"/>
  </bean >

  <bean name = "wheelReference" class = "Wheel">
   <property name = "wheel" value="Thick Tire"/>
  </bean >

</beans>

Since, the 'Car' class is holding a 'Wheel' reference


class Car {

  Wheel wheel;
  ...
  ...
}

We have instantiated 'Wheel' first (Also, initialized the 'tireType' property with value 'Thick Tire'):


<bean name = "wheelReference" class = "Wheel">
   <property name = "tireType" value="Thick Tire"/>
</bean >

Just note we have put the '<bean name =..>' as 'wheelReference'.

Then, we have taken this 'wheelReference' and put it in the 'ref' of 'wheel' property. And instantiated the 'Car'.


<bean name = "car" class = "Car">
   <property name = "wheel" ref = "wheelReference"/>
</bean >

So, spring is using the setter method of 'Wheel', which belongs to 'Car' class to get the Setter injection done.


public void setWheel(Wheel wheel) {
  this.wheel = wheel;
  }

Finally, in the main() method we define our application code to interact with spring.


import org.springframework.context.ApplicationContext;

public class FirstSpringApp {
  public static void main(String[] args) {

  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");

  Car car = (Car)applicationContext.getBean("car");

  System.out.println("The Car brand is : "+car.getBrand());

  }
}

Just note, we have just asked spring to give us a 'Car' object.


Car car = (Car)applicationContext.getBean("car");

And in the background, spring has created the 'Wheel' object and injected into the 'Wheel' reference of 'Car' class.