Setter injection

The dependencies of an object are fulfilled by the setter methods in setter injection. So, the very important thing when we use setter injection for DI for a bean, is that the data members will be set through the setter methods. The steps to use the setter injection are as follows:

  1. Declare a class and its properties using the standard Java naming convention.
  2. Configure the bean in bean definition XML as follows:
<bean id="bean_id" class="fully qualified 
_class_name"></bean>
  1. The bean in the preceding configuration creates the instance. It has to be updated to configure the properties like this:
    • Each <property> tag will configure a data member
    • Each <property> tag has two attributes as follows:
      • name: The name attribute specifies the name of the data member whose value the developer wants to configure
      • value: The value attribute specifies the value to be given to the data member

The updated configuration will be as follows:

<bean id="bean_id" class="fully qualified _class_name"> 
  <property name="name_of_property"
value="value_of_property"> </bean>

If we have more than one data member (dependencies) whose values are to be set, we will need to use more than one <property> tag.

  1. Get the bean from the Spring container, and you are ready to use it.

First, let's find out how to configure a bean with the setter injection by following these steps:

  1. Create Ch02_Dependency_Injection as a Java project.
  2. Add to it all the core Spring jars that we have already used in the previous chapter.
  3. Create a Car class in com.ch2.beans.You can refer to the code from the previous project, that is, Ch02_Instance_Creation.
  4. As we will inject the dependencies using setter injection, create setter methods as well.
  5. Also add the show() method.

The code will be as follows:

public class Car { 
  private String chassis_number, color, fuel_type; 
  private long price; 
  private double average; 
 
  //now we write getters and setters for the data members 
  public void setChassis_number(String  
  chassis_number) { 
    this.chassis_number = chassis_number; 
   } 
 
   public void setColor(String color) { 
     this.color = color; 
   } 
 
   public void setFuel_type(String fuel_type) { 
    this.fuel_type = fuel_type; 
   } 
 
   public void setPrice(long price) { 
     this.price = price; 
   } 
 
   public void setAverage(double average) { 
     this.average = average; 
   } 
 
   // all getters and setters have been defined now. Please see  the tip below 
 
   public void show() 
   { 
     System.out.println("showing car "+chassis_number+" having
color:-"+color+"and price:-"+price); } }
Make sure that you follow the Bean naming convention whenever you create a class to configure in Spring Container. Also, you may have noticed the getters and setters in the preceding code. They are a very simple concept to grasp. You can look it up on the Internet for more information. However, going forward, we won't be adding getters and setters in this chapter (though they will be present in the code files!).

The state of an object can be obtained using getter methods. So, normally, the developers add both getters and setters. Adding getters always depends on the business logic of the application.

  1. Now, we will need to configure the bean definition in beans.xml in classpath, which represents a bean definition so that the bean is managed by the Spring container. The configuration will be as follows:
<bean id="car_obj" class="com.ch02.beans.Car"/> 
  1. In the previous step, only instance is created; now, we want to set the properties of it as well, which is the process of injecting dependencies using setter. The code will be as follows:
<bean id="car_obj" class="com.ch02.beans.Car"> 
<property 
name="chassis_number" value="eng2012"/> 
<property name="color" value="baker's chocolate"/> 
<property name="fuel_type" value="petrol"/> 
<property name="average" value="12.50"/> 
<property name="price" value="643800"/> 
</bean> 

If we don't want to set the values of any dependency, the simple thing is not to add it in the configuration.

  1. Now we are ready to use the bean. We have to ask the container to give the object of the bean. Create a TestCar class in the default package with the main function. The code will look as follows:
public class TestCar { 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    ApplicationContext context=new 
      ClassPathXmlApplicationContext("beans.xml"); 
    Car car=(Car)context.getBean("car_obj"); 
    car.show(); 
  } 
} 
  1. On execution of the code, we will get the following output:

The values shown in the figure are the same as those that we set from the configuration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.9